About
A command line argument parser in the Mys programming language.
Examples
from argparse import Parser
func main(argv: [string]):
parser = Parser("basic",
version=__version__,
help="A basic example.")
parser.add_option("--verbose",
short="-v",
multiple_occurrences=True,
help="Verbose output.")
parser.add_positional("file",
help="A file.")
args = parser.parse(argv)
print("File: ", args.value_of("file"))
print("Number of --verbose: ", args.occurrences_of("--verbose"))
Build and run:
❯ mys run -- --verbose foobar.txt
✔ Reading package configuration (0 seconds)
✔ Building (0.01 seconds)
File: foobar.txt
Number of --verbose: 1
Show help:
❯ mys run -- --help
✔ Reading package configuration (0 seconds)
✔ Building (0.01 seconds)
Synopsis
basic [options] <file>
Description
A basic example.
Positionals
file A file.
Options
-h, --help Show this help.
--version Show version information.
--shell-completion Print the shell command completion script.
-v, --verbose Verbose output.
Generate Zsh completions:
❯ ln -s build/speed/app basic
❯ export PATH=$(pwd):$PATH
❯ basic --shell-completion zsh > ~/.oh-my-zsh/functions/_basic # Somewhere in $FPATH
❯ compinit
❯ basic -<TAB>
--help -h -- Show this help.
--shell-completion -- Print the shell command completion script.
--verbose -v -- Verbose output.
--version -- Show version infomation.
API
enum ValueType(i64):
Value type for completion scripts.
Path
FilePath
DirPath
Hostname
Other
class ArgparseError(Error):
message: string
class Args:
Returned by the parser's parse method.
remaining: [string]
func __init__(self,
options: {string: i64},
single_values: {string: string},
multiple_values: {string: [string]},
subcommand: (string, Args)?):
func is_present(self, arg: string) -> bool:
Returns true if given presence (boolean) option was given, false
otherwise.
func occurrences_of(self, arg: string) -> i64:
Returns the number of times given presence (boolean) option was
given.
func value_of(self, arg: string) -> string:
Returns the value of given option or positional. Raises an error
if given option does not take a value or if given positional can be
given multiple times.
func values_of(self, arg: string) -> [string]:
Returns a list of values of given multiple occurrences option or
positional.
func subcommand(self) -> (string, Args):
Returns a tuple of the subcommand and its arguments.
class Parser:
An argument parser.
name: string?
help: string?
version: string?
func __init__(self,
name: string? = None,
help: string? = None,
version: string? = None,
parent: Parser? = None):
func add_option(self,
name: string,
short: string? = None,
takes_value: bool = False,
value_type: ValueType = ValueType.Other,
default: string? = None,
multiple_occurrences: bool = False,
choices: [string]? = None,
help: string? = None):
Add an option. Options must be added before subcommands and
positionals.
func add_positional(self,
name: string,
value_type: ValueType = ValueType.Other,
multiple_occurrences: bool = False,
choices: [string]? = None,
help: string? = None):
Add a positional. Positionals cannot be mixed with subcommands.
func add_subcommand(self, name: string, help: string? = None) -> Parser:
Add a subcommand. Subcommands cannot be mixed with positionals.
func parse(self,
argv: [string],
exit_on_error: bool = True,
allow_remaining: bool = False) -> Args:
Parse given arguments and return them.
Give exit_on_error as False to raise an exception instead of
exiting if an error occurs.
Give allow_remaining to allow more non-option arguments than
the parser expects. Remaining arguments are part of the
returned value.
func zsh_completion(self) -> string:
Returns the Zsh command completion script.