2

I am using the nuget package Command Line Parser for parsing command line arguments in C#.

How do I pass a GUID from command line?

ApplicationName.exe -g="3a0e5412-0971-4e0e-aebc-29dd09907b31"

does not work.

My CommandLineArgs class is

[Option('g', "sampleguid", Required = true, HelpText = "Enter a sample GUID")]
public Guid MyGuid { get; set; }
Rockstart
  • 2,337
  • 5
  • 30
  • 59
  • 2
    Can you describe "does not work"? What is the result? Is there an exception? – mason Oct 28 '14 at 16:44
  • Parser.Default.ParseArguments(args, commandLineArgs) returns false – Rockstart Oct 28 '14 at 16:49
  • Check the documentation and see if there's a way to check the actual reason why it returned false. You might even be able to find it by rooting around in Intellisense. – mason Oct 28 '14 at 16:56

3 Answers3

2

First, there is no built-in functionality for Guid. Second, make sure you're using the latest version 2.6.0.5. You can install it through nuget -> search for "CommandLineArgumentsParser".

Once you've installed the latest version, you can interpret custom structures as such:

var parser = new CommandLineParser.CommandLineParser();

var guidArgument = new ValueArgument<Guid>('g', "guid", "Guid of something");
guidArgument.ConvertValueHandler = Guid.Parse;

parser.Arguments.Add(guidArgument);

parser.ParseCommandLine(args);

// the actual guid from command line.
var parsedGuid = guidArgument.Value;

If you want to keep your current version, you need to treat Guid as string when parsing, and later do custom validation by yourself.

http://commandlineparser.codeplex.com/wikipage?title=More%20thorough%20examples&referringTitle=Home

Erti-Chris Eelmaa
  • 25,338
  • 6
  • 61
  • 78
1

Have you tried dropping the "=" and using a " " (space) instead?

Like: ApplicationName.exe -g "3a0e5412-0971-4e0e-aebc-29dd09907b31"

GeorgDangl
  • 2,146
  • 1
  • 29
  • 37
  • +1: In the land of Unix/Linux, arguments attached to "short options" must be specified as either `-x ` or "-x". If the app supports *argument bundling*, where short options are concatenated together (e.g., `-a -b -c` can be specified as `-abc`, the option that takes the argument must be the last option in the bundle and the argument is either the remainder of the spec (`-abcx` or the next work on the command line (`-abcx `. – Nicholas Carey Oct 28 '14 at 17:33
1

As you can see from GUID is not working for input parameter, the Guid parsing issue was already fixed. (However, if you use Guid?, it still does not work.)

Ping Jin
  • 520
  • 4
  • 8