Skip to content

bovorasr/CommandLine

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

89 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CommandLine

CommandLine parser

NuGet version Build status codecov MIT License

Getting started

Here is how you can define a class that can be used with the parser:

class Options
{
    [RequiredArgument(0,"dir","Directory")]
    public string Directory { get; set; }

    [OptionalArgument("*.*","pattern", "Search pattern")]
    public string SearchPattern { get; set; }
}

This is the code that you need to parse the command line arguments into an object:

if (!Parser.TryParse(args, out Options options))
{
    return;
}

The parser understands common requests for help as you can see below.

Display the available commands

The parser understand the following help commands:

  • -? which is the short form help
Usage:
<exename> dir [-pattern value]

For detailed information run '<exename> --help'.
  • --help which is the long form help
Usage:
<exename> dir [-pattern value]
 - dir     : Directory (string, required)
 - pattern : Search pattern (string, default=*.*)

Display the help using code

If you want to display the help manually, you can use the Parser.DisplayHelp() API to do it:

Parser.DisplayHelp<Options>();

By default, the API assumes you are using the HelpFormat.Short specifier which maps to /? or -?.

You can ask for the long version of the help by using the HelpFormat.Long specifier:

Parser.DisplayHelp<Options>(HelpFormat.Long);

Types of arguments

To use you need to provide a class that is going to hold the options parsed from the command line and use attributes to define the behavior of the parser.

There are 2 kinds of arguments:

  • Required
    • These must be specified
    • They have a specific location where they must be specified
  • Optional
    • They don't have to be specified
    • They have a name that you need to provide on the command line before you provide the value
    • If they are not specified, the object parsed will have the default value specified.'

Advanced scenarios

There are cases when you want to have different sets of arguments depending on one of the arguments.

The argument that will be used as the differentiator will be marked with the ActionArgument attribute. That argument must be specified.

You can then define multiple groups of arguments. You will specify the argument group for a property using the ArgumentGroup attribute. A single property in a class can take part in many argument groups.

If a property will be common to all groups, you can use the `CommonArgument' attribute

internal class CommandLineOptions
{
    [ActionArgument]
    public CommandLineActionGroup Action { get; set; }

    [CommonArgument]
    [RequiredArgumentAttribute(0, "host", "The name of the host")]
    public string Host { get; set; }

    #region Add-host Action
    [RequiredArgument(1, "mac", "The MAC address of the host")]
    [ArgumentGroup(nameof(CommandLineActionGroup.add))]
    public string MAC { get; set; }

    #endregion
}

About

CommandLine parser

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 97.2%
  • PowerShell 2.0%
  • Other 0.8%