Argparse.Parser
A module for parsing arguments from the command line. With this you can turn the following
mycmd greet --overwrite file.txt
Into a data structure.
mycmdis the Applicationgreetis the Command--overwriteis a Flagfile.txtis an Argument
Application
Defines a command line application.
nameis the name of the applicationversionis the application versioncommandsis a group of commands that this application recognizesintroandoutrois used when the user calls the application without any arguments. They define what text comes before and after the list of supported commands.
Takes input from the user in the form of an Array of String, and parses it according
to the given App definition.
Parses tokens against a single Command, without first
consuming a command word. Use this to build a tool that has no sub-commands —
just flags and arguments, like mytool --loud World — where run would
otherwise treat the first token (--loud) as a command name.
appName is only used to render the usage line in --help output. The command
word in --help comes from spec.word; pass an empty word for a rootless
tool so the usage line reads mytool <args>. --help (and its alias -h) is
handled here (it yields HelpText); --version, being application-level,
is not — intercept it yourself, as Argparse.Program.runRoot
does.
Command
Definition of a command.
word— the sub-command word the user types (e.g."add"→todo add …).arguments— the positional arguments this command accepts.flags— the flags this command accepts.commonDescription— ifJust, a one-line description shown next to this command in the app-level--helplisting. PassNothingto hide the command from that listing.summary— a sentence or two shown at the top of this command's own--helppage.example— a usage example rendered below the summary on this command's--helppage.builder— converts the parsed arguments and flags into your own command type.
The result of parsing user input.
Groups
A parser for a group, or collection, of commands. Build one with defineGroup, then pipe it through withCommand and withPrefix to register each command word:
commands : Argparse.Parser.GroupParser Command
commands =
Argparse.Parser.defineGroup
|> Argparse.Parser.withCommand addCommand -- word = "add"
|> Argparse.Parser.withCommand listCommand -- word = "list"
|> Argparse.Parser.withPrefix "remote" remoteCommands
Given an app named "mytool", the resulting CLI accepts:
mytool add ...
mytool list
mytool remote ...
Pass the resulting GroupParser as the commands field of App.
Defines an empty group of commands. To add commands to this group, you'll need to use withCommand and withPrefix.
Returns a new group that contains the given command. If there are multiple
commands in the group with the same word, then the last registered command
wins.
Argparse.Parser.defineGroup
|> Argparse.Parser.withCommand
{ word = "greet"
, arguments = Argparse.Parser.oneArg { value = Argparse.Parser.stringParser, help = "Name to greet" }
, flags = Argparse.Parser.noFlags
, commonDescription = Just "Say hello."
, summary = "The `greet` command prints a greeting:"
, example = PP.words "mytool greet Alice"
, builder = \name _flags -> Greet name
}
This makes mytool greet Alice a valid invocation.
Embeds a group of commands into an existing group, accessible by the given prefix.
remoteCommands : Argparse.Parser.GroupParser Command
remoteCommands =
Argparse.Parser.defineGroup
|> Argparse.Parser.withCommand addRemoteCommand -- word = "add"
|> Argparse.Parser.withCommand listRemoteCommand -- word = "list"
Argparse.Parser.defineGroup
|> Argparse.Parser.withPrefix "remote" remoteCommands
This makes mytool remote add ... and mytool remote list valid invocations.
mytool remote --help lists the commands under the prefix.
Arguments
Defines how a raw string argument is converted to a typed value.
label— the placeholder name shown in usage lines, e.g."count"renders as<count>fn— converts the raw string to the target type; returnNothingto signal a parse failureexamples— one or more example values shown in--helpoutput
For example, an integer parser:
intParser : Argparse.Parser.ValueParser Int
intParser =
{ label = "count"
, fn = String.toInt
, examples = [ "1", "10" ]
}
-- mytool run --limit 5
Argparse.Parser.flag (Argparse.Parser.LongOnly "limit") intParser "Maximum number of items"
A single positional argument: a ValueParser plus a help
string describing what the argument means. The help is shown, per argument, in
the command's --help output.
Arguments are values that a Command requires to function. An ArgumentParser
is a way of converting an arbitrary number of String inputs into Gren values.
There are mainly two things that can go wrong when parsing arguments. You can either get an unexpected number of arguments or one or more of those arguments may fail to parse.
Turns an ArgumentParserError into a prettified String.
Some Commands doesn't require arguments. This parser checks that exactly 0 arguments were provided.
Parses exactly one argument.
Parses exactly two arguments.
Parses exactly three arguments.
Parses zero or more arguments of a single type.
Parses one or more arguments of a single type. Like zeroOrMoreArgs, but fails if no arguments are given.
Parses an optional single argument: zero arguments yields Nothing, exactly
one yields Just the parsed value, and anything more is an error.
Maps a successfully parsed set of arguments into something else.
A list of parsers that will be tried one after another until one succeeds.
Flags
Flags are prefixed with -- and are meant to customize a command.
Build a FlagParser by seeding initFlags with a record constructor,
then chaining toggle and flag:
type alias MyFlags =
{ verbose : Bool
, output : Maybe Path
}
myFlags : Argparse.Parser.FlagParser MyFlags
myFlags =
Argparse.Parser.initFlags (\verbose output -> { verbose = verbose, output = output })
|> Argparse.Parser.toggle
(Argparse.Parser.Both { long = "verbose", short = "v" })
"Show detailed output"
|> Argparse.Parser.flag
(Argparse.Parser.LongOnly "output")
Argparse.Parser.pathParser
"Write output to this file"
This accepts --verbose (or -v) and --output <path>:
mytool run --verbose --output build/result.txt
The name(s) by which a flag is known on the command line. A flag may have
a long form (--verbose), a short form (-v), or both. At least one must be
provided when calling toggle or flag.
There are several things that can go wrong when parsing flags:
- The parser can find an associated value for a flag that requires none.
- The parser might not find an associated value for a flag that does require one.
- The associated value might fail to parse.
- The flag might be unknown.
Returns a prettified error message for the given FlagParserError.
A parser that verifies that no flags have been provided.
Defines an empty group of flags. You'll need to register flags using the toggle and flag functions.
Defines a flag that doesn't require an associated value. We're simply checking if the flag is present or not.
Defines a flag that requires an associated value.
Common parsers
For FileSystem.Path, like /some/path/file.txt.
Note: expects posix paths.
For any plain String value.
For any Int value.