Skip to content

Commit 3e79891

Browse files
committed
Merge pull request scriptcs#518 from per-samuelsson/issue/515
Fixing scriptcs#515 - allowing non-script arguments to the CLI to be properly handled again
2 parents fec0f1a + d9dd2c5 commit 3e79891

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

src/ScriptCs/Program.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,24 @@ private static int Main(string[] args)
3131

3232
if (string.IsNullOrWhiteSpace(extension) && !commandArgs.Repl)
3333
{
34+
// No extension was given, i.e we might have something like
35+
// "scriptcs foo" to deal with. We activate the default extension,
36+
// to make sure it's given to the LoadModules below.
3437
extension = ".csx";
35-
var scriptName = string.Format("{0}.csx", commandArgs.ScriptName);
3638

37-
if (!File.Exists(scriptName))
39+
if (!string.IsNullOrWhiteSpace(commandArgs.ScriptName))
3840
{
39-
console.WriteLine(string.Format(
40-
"Can't find a script named {0}",scriptName));
41-
42-
return 1;
41+
// If the was in fact a script specified, we'll extend it
42+
// with the default extension, assuming the user giving
43+
// "scriptcs foo" actually meant "scriptcs foo.csx". We
44+
// perform no validation here thought; let it be done by
45+
// the activated command. If the file don't exist, it's
46+
// up to the command to detect and report.
47+
48+
commandArgs.ScriptName += extension;
4349
}
44-
45-
commandArgs.ScriptName = scriptName;
4650
}
4751

48-
4952
scriptServicesBuilder.LoadModules(extension, modules);
5053
var scriptServiceRoot = scriptServicesBuilder.Build();
5154

test/ScriptCs.Tests/ArgumentHandlerTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,20 @@ public void ShouldHandleCustomConfigFile()
132132
result.ScriptArguments.ShouldEqual(new string[] { "-port", "8080" });
133133
result.CommandArguments.Install.ShouldEqual("install test value");
134134
}
135+
136+
[Fact]
137+
public void ShouldHandleHelp()
138+
{
139+
string[] args = { "-help" };
140+
141+
var argumentHandler = Setup(null);
142+
var result = argumentHandler.Parse(args);
143+
144+
result.ShouldNotBeNull();
145+
result.Arguments.ShouldEqual(args);
146+
result.CommandArguments.ScriptName.ShouldBeNull();
147+
result.CommandArguments.Help.ShouldBeTrue();
148+
}
135149
}
136150
}
137151
}

test/ScriptCs.Tests/ArgumentParserTests.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,20 @@ public void ShouldHandleNull()
4545
result.LogLevel.ShouldEqual(LogLevel.Info);
4646
result.Config.ShouldEqual("scriptcs.opts");
4747
}
48-
}
48+
49+
[Fact]
50+
public void ShouldSupportHelp()
51+
{
52+
string[] args = { "-help" };
53+
54+
var parser = new ArgumentParser(new ScriptConsole());
55+
var result = parser.Parse(args);
56+
57+
result.ShouldNotBeNull();
58+
result.ScriptName.ShouldBeNull();
59+
result.Help.ShouldBeTrue();
60+
result.LogLevel.ShouldEqual(LogLevel.Info);
61+
}
62+
}
4963
}
5064
}

0 commit comments

Comments
 (0)