Skip to content

[WIP] Subcommand support, grouped switches and many other improvements and refactorings for Argu v.3 - #54

Merged
eiriktsarpalis merged 71 commits into
masterfrom
argu-3
Jun 30, 2016
Merged

eiriktsarpalis merged 71 commits into
masterfrom
argu-3

Conversation

@eiriktsarpalis

@eiriktsarpalis eiriktsarpalis commented Jun 18, 2016 •

Copy link
Copy Markdown
Member

This PR introduces significant changes to the library in what will be Argu version 3. Here are some of the main changes:

Subcommand support

Introduces support for nested union arguments. For instance:

type PushArgs =
    | Remote of name:string
    | Branch of name:string
with
    interface IArgParserTemplate with
        member this.Usage = "..."

type GitArgs =
    | [<AltCommandLine("-v")>]Verbose
    | [<CliPrefix(CliPrefix.Empty)>]Push of ParseResult<PushArgs>
with 
    interface IArgParserTemplate with 
        member this.Usage = "..."

This can be used to provide contextual arguments based on the initial argument. For instance:

let parser = ArgumentParser.Create<GitArgs>()
let result = parser.Parse [|"-v" ; "push" ; "--remote" ; "origin" ; "--branch" ; "master"|]
let nestedResult = result.GetResult <@ Push @>
nestedResult.GetAllResults() // [Remote "origin" ; Branch "master"]

Calling git push --help instead of git --help will result in contextual help text being displayed. To give you an example, this template now generates the following help text:

Gadget -- my awesome CLI tool

USAGE: gadget [--listener <address> <number>] [--log-level <level>] [--environment-variable <key>=<value>] [--ports <int> ...] [push <subcommands>] [clean <subcommands>] [--help]

OPTIONS:

    --listener <address> <number>: Specify a listener host/port combination.
    --log-level <level>: Specify a log level for the process.
    --environment-variable [-E] <key>=<value>: Specifies an environment variable for the process.
    --ports <int ...>: Specifies a collection of port for the process.
    --help: display this list of options.

SUBCOMMANDS:

    push <options>: Pushes changes to remote repo. See 'gadget push --help' for more info.
    clean <options>: Cleans the local repo. See 'gadget clean --help' for more info.

    Use 'gadget <subcommand> --help' for additional information.

whereas using the contextual help parameter results in

Gadget -- my awesome CLI tool

USAGE: gadget clean [-d] [-f] [-x] [--help]

OPTIONS:

    -d: Remove untracked directories in addition to untracked files
    -f [--force]: If the Git configuration variable clean.requireForce is not set to false, git clean will refuse to delete files or directories unless given -f.
    -x: Remove only files ignored by Git. This may be useful to rebuild everything from scratch, but keep manually created files.
    --help: display this list of options.

Grouped Switches

Adds support for grouping of switches, i.e. arguments that take zero parameters and are of the form -f or -X. For instance the template

[CliPrefix(CliPrefix.SingleDash)]
type CleanArgs = F | D | X

now recognizes arguments of the form -fdx as being equivalent to -f -d -x.

Optional and Variadic parameters

Adds support for parameters that take optional, or list parameters. For instance

type Argument =
    | Optional of int option
    | List of string list

can be used to parse command line arguments of the form --optional, --list 1 2 3, --optional 2 and --list. Note that optional/variadic parameters can only occur in arguments of arity 1. Also note that the parsing strategy of Argu has been slightly modified in order to accommodate this feature.

EqualsAssignment support for arity 2

Adds support for the EqualsAssignment attribute in parameters of arity 2. For example

type Argument =
     | [<EqualsAssignment>] Environment_Variable of key:string * value:string

which can be used to parse expressions of the form --environment-variable key=value

Misc Improvements

  • UniqueAttribute for declaring that parameter should be specified at most once. Dual to the MandatoryAttribute.
  • HelpFlagsAttribute, HelpDescriptionAttribute and DisableHelpFlagsAttribute can be used to control the look of the --help parameters.
  • All exceptions raised by the library are now of type ArguException.
  • The PrintLabelsAttribute which takes advantage of F# 3.1 DU labels is now enabled by default.
  • Migrate tests to XUnit from NUnit. Expand unit test coverage.
  • Add a ParseResult.UnrecognizedCliParams property which collects all CLI parameters not recognized by the parser. This also addresses issue Gather unrecognized arguments #55.

@jackfoxy

Copy link
Copy Markdown

I could not find an issue to comment on this topic.
My related suggestions:
Granular usage (help) by command, perhaps also supporting separate examples by command.

@cloudRoutine

Copy link
Copy Markdown
Member

Will the usage strings on subcommands be in attributes?
Could there be a verbose/-v flag and corresponding attribute for more detailed help output?

@haf

haf commented Jun 30, 2016

Copy link
Copy Markdown
Member

🎆 🎂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants