Skip to content

Commit

Permalink
support command line arguments without double quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
maximpashuk authored and phatboyg committed Oct 16, 2016
1 parent 4312b6c commit 66afad9
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 4 deletions.
76 changes: 76 additions & 0 deletions src/Topshelf.Tests/CommandLine_Specs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,21 @@ public void Should_create_an_install_host_with_service_name()
Assert.AreEqual("Joe", installHost.Settings.ServiceName);
}

[Test]
public void Should_create_an_install_host_with_service_name_no_quotes()
{
Host host = HostFactory.New(x =>
{
x.Service<MyService>();
x.ApplyCommandLine("install -servicename Joe");
});

Assert.IsInstanceOf<InstallHost>(host);
var installHost = (InstallHost)host;
Assert.AreEqual("Joe", installHost.Settings.Name);
Assert.AreEqual("Joe", installHost.Settings.ServiceName);
}

[Test]
public void Should_create_an_install_host_with_display_name()
{
Expand Down Expand Up @@ -101,6 +116,20 @@ public void Should_create_an_install_host_with_display_name_and_instance_name()
Assert.AreEqual("Joe (Instance: 42)", installHost.Settings.DisplayName);
}

[Test]
public void Should_create_an_install_host_with_display_name_and_instance_name_no_quotes()
{
Host host = HostFactory.New(x =>
{
x.Service<MyService>();
x.ApplyCommandLine("install -displayname Joe -instance 42");
});

Assert.IsInstanceOf<InstallHost>(host);
var installHost = (InstallHost)host;
Assert.AreEqual("Joe (Instance: 42)", installHost.Settings.DisplayName);
}

[Test]
public void Should_create_an_install_host_with_display_name_with_instance_name()
{
Expand All @@ -115,6 +144,20 @@ public void Should_create_an_install_host_with_display_name_with_instance_name()
Assert.AreEqual("Joe (Instance: 42)", installHost.Settings.DisplayName);
}

[Test]
public void Should_create_an_install_host_with_display_name_with_instance_name_no_quotes()
{
Host host = HostFactory.New(x =>
{
x.Service<MyService>();
x.ApplyCommandLine("install -displayname \"Joe (Instance: 42)\" -instance 42");
});

Assert.IsInstanceOf<InstallHost>(host);
var installHost = (InstallHost)host;
Assert.AreEqual("Joe (Instance: 42)", installHost.Settings.DisplayName);
}

[Test]
public void Should_create_an_install_host_with_description()
{
Expand Down Expand Up @@ -145,6 +188,22 @@ public void Should_create_an_install_host_with_service_name_and_instance_name()
Assert.AreEqual("Joe$42", installHost.Settings.ServiceName);
}

[Test]
public void Should_create_an_install_host_with_service_name_and_instance_name_no_quotes()
{
Host host = HostFactory.New(x =>
{
x.Service<MyService>();
x.ApplyCommandLine("install -servicename Joe -instance 42");
});

Assert.IsInstanceOf<InstallHost>(host);
var installHost = (InstallHost)host;
Assert.AreEqual("Joe", installHost.Settings.Name);
Assert.AreEqual("42", installHost.Settings.InstanceName);
Assert.AreEqual("Joe$42", installHost.Settings.ServiceName);
}

[Test]
public void Should_create_and_install_host_with_service_name_containing_space()
{
Expand Down Expand Up @@ -319,6 +378,23 @@ public void Need_to_handle_crazy_special_characters_in_argument()
Assert.AreEqual("abc123=:,.<>/?;!@#$%^&*()-+", password);
}

[Test]
public void Need_to_handle_crazy_special_characters_in_argument_no_quotes()
{
string password = null;

Host host = HostFactory.New(x =>
{
x.Service<MyService>();

x.AddCommandLineDefinition("password", v => password = v);

x.ApplyCommandLine("-password abc123=:,.<>/?;!@#$%^&*()-+");
});

Assert.AreEqual("abc123=:,.<>/?;!@#$%^&*()-+", password);
}

[Test]
public void Extensible_the_command_line_should_be_yet_again()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ from cs in Rep(Char(char.IsLetterOrDigit).Or(Char('.')))
Value = (from symbol in Rep(Char(char.IsLetterOrDigit).Or(Char(char.IsPunctuation)).Or(Char(char.IsSymbol)))
select symbol.Aggregate("", (s, ch) => s + ch));

ValueInQuotes = from oq in Char('"')
from value in Rep(EscChar)
from cq in Char('"')
select value.Aggregate("", (s, ch) => s + ch);

Definition = (from w in Whitespace
from c in Char('-').Or(Char('/'))
from key in Id
Expand All @@ -51,10 +56,8 @@ select DefinitionElement.New(key, value))
from c in Char('-').Or(Char('/'))
from key in Id
from ws in Whitespace
from oq in Char('"')
from value in Rep(EscChar)
from cq in Char('"')
select DefinitionElement.New(key, value.Aggregate("", (s, ch) => s + ch)));
from value in ValueInQuotes.Or(Value)
select DefinitionElement.New(key, value));

EmptyDefinition = (from w in Whitespace
from c in Char('-').Or(Char('/'))
Expand Down Expand Up @@ -107,6 +110,7 @@ from c in Char(']')
Parser<string, string> Id { get; set; }
Parser<string, string> Key { get; set; }
Parser<string, string> Value { get; set; }
Parser<string, string> ValueInQuotes { get; set; }

Parser<string, ICommandLineElement> Definition { get; set; }
Parser<string, ICommandLineElement> EmptyDefinition { get; set; }
Expand Down

0 comments on commit 66afad9

Please sign in to comment.