"""
The interface for managing the creation of command line argument specifications
and parsing them.
- Author: Casey Walker
- License: MIT
Example usage:
```python
.. include:: ../../examples/say-hello
```
"""
"""
A base class for defining argument types that can be parsed from command
line arguments.
"""
"""
# Parameters:
- `is_unrestricted`:
Whether or not the argument specification can consume prefixed
arguments (e.g. it can consume potential floating arguments). This
is useful for defining a "sink" argument like `"--"`.
- `desc`:
The string describing how the argument should be used. This can be
useful for generating usage documentaion.
"""
=
=
"""
Return whether the argument specification can consume more arguments
given a number of arguments that have already been consumed.
# Parameters:
- `prefix`:
The prefix the occurance of the argument was found with. Subclasses
may find this useful for determining consumption behavior.
- `num_consumed`:
The number of arguments that have already been consumed.
# Returns:
`True` if more arguments can be consumed, `False` otherwise.
"""
"""
Consume arguments from a list of available, consumable arguments.
# Parameters:
- `prefix`:
The prfix the occurance of the argument was found with. Subclasses
may be able to use this to determine consumption behavior.
- `consumable_strs`:
The consumable argument strings to go through.
# Yields:
The string that was consumed.
# Notes:
Argument specifications are not required to consume all available
strings, however it is required that returned strings *must* be in order
in an unbroken chain. In other words, consumed arguments must be
consecutive without skipping.
"""
= 0
break
yield
+= 1
"""
An argument specification that allows multiple arguments to be consumed.
This serves as the basis for the typical floating and positional arguments.
It simply allows for argument consumption based on a count (which may be
unlimited).
"""
"""
# Parameters:
- `num_args`:
The number of arguments to consume when the specification is
encountered. The sign of the number is ignored during consumption,
but it is preserved so it can used for special purposes if desired.
- `is_unbounded`:
Whether or not the specification can consume unlimited arguments.
This overrides specifying a number of arguments to consume, but the
information is still preserved in case it's useful post-parsing.
- `is_unrestricted`:
Whether or not the specification can consume prefixed arguments.
- `desc`:
The description of the argument.
# Notes:
Here are a couple ways to use extra information provided by the number
of arguments and the boundedness of a specification when iterating
through parsed arguments:
- One way to use a negative number of arguments is to have it determine
a maximum number of arguments but allow for fewer. In other words,
positive means the exact number must be satisfied, but negative
means fewer are acceptable.
- Specifying the number of arguments can be combined with unboundedness
by treating the number of arguments as a minimum number of
acceptable arguments.
"""
=
=
return True
return <
"""
An optional argument which is prefixed and may be located anywhere in the
list of arguments (i.e. not positional).
Floating arguments may have short and long forms, where short forms are
prefixed by a short prefix and long forms are prefixed by a long prefix.
Prefixes are determined by the parser. Floating arguments are analagous to
keyword arguments in programming languages.
"""
"""
# Parameters:
- `short`:
The short form of the specification. May be a string of more than
one character to allow multiple short strings for the same
specification. If `None`, no short form will be matched.
- `long`:
The long form of the specification. If `None`, no long form will be
matched.
- `num_args`:
The number of arguments to consume.
- `is_unbounded`:
Whether or not to consume an unbounded number of arguments.
- `is_unrestricted`:
Whether or not to consume prefixed arguments.
- `desc`:
The description of the argument.
# Notes:
To create a floating argument that only consists of a prefix character
(e.g. `'-'` or `"--"`), pass `short` and/or `long` as `""` (an empty
string).
"""
=
=
"""
Return if a string matches the specification's short form.
"""
return in
"""
Return if a string matches the specification's long form.
"""
return ==
return f
"""
A positional argument that occurs in a specific order relative to other
positional arguments.
Positional arguments can be interspersed with floating arguments.
"""
"""
# Parameters:
- `name`:
The name of the positional argument. This can be useful for
generating usage documentation.
- `num_args`:
The number of arguments to consume for this position. If given as
`0`, the specification is simply ignored.
- `is_unbounded`:
Whether or not to consume an unbounded number of arguments.
- `is_unrestricted`:
Whether or not to consume prefixed arguments.
- `desc`:
The description of the positional argument, which may be useful for
generating usage documentation.
"""
=
return f
"""
A parsed argument consisting of the various parts it was found with and the
specification that it was matched with.
"""
:
"""The argument specification that was matched."""
:
"""The associated values that were found for the specification."""
: | None = None
"""The prefix that was found if it was floating."""
: | None = None
"""The floating form that was matched if it was floating."""
"""
An argument parser, primarily for parsing command line arguments.
"""
=
"""The specification that captures the program name."""
=
"""The specification that captures unknown floating arguments."""
=
"""The specification that captures extra positional arguments."""
"""
# Parameters:
- `specs`:
The argument specifications to expect during parsing. Any positional
specifications present will be ordered as encountered during
iteration through the specifications.
- `progname`:
The name of the program. It may be useful for generating usage
documentation.
- `short_prefixes`:
The prefixes to check for short floating arguments.
- `long_prefixes`:
The prefixes to check for long floating arguments.
- `desc`:
The description of the program. This may be used in generating usage
documentation.
"""
:
:
:
=
=
=
=
"""
Initialize the instance's specification lists.
The lists are sorted into one for positional specifications, one for
floating specifications, and one for any custom specifications that are
subclasses of `Arg_spec`. The lists are ordered by order of encounter
(i.e. the first positional specification that is encountered during
iteration is appended first, etc.).
# Parameters:
- `specs`:
The argument specifications to sort into respective lists. This
should be passed directly from `__init__` without modication.
# Notes:
This function is meant to extend `__init__` and should thus only ever be
called once, during instance initialization.
"""
: =
: =
: =
:
:
:
:
=
=
=
"""
Return whether an argument begins with one of the prefixes.
"""
return
"""
Strip an argument of its prefix.
# Parameters:
- `arg`:
The argument to strip the prefix from.
- `prefixes`:
The potential prefixes to strip from the argument.
# Returns:
The prefix that was stripped and the stripped argument. If no prefix
could be stripped, the prefix is returned as `None` and the argument is
returned unmodifified.
"""
return
return
"""
Return whether an argument belongs to one of the special specifications
given during initialization.
This method is meant to be overridden by subclasses that wish to use
specially-subclassed versions of `Arg_spec`. Overridden methods should
return whether an argument is considered to be special or not.
# Parameters:
- `arg`:
The argument to test.
- `args`:
The rest of the unparsed arguments. This is given to provide as much
context as possible to overriding methods that may need more than
just the argument itself to determine if it is special.
# Returns:
`True` if the argument is special, and `False` otherwise. The default
implementation will always return `False`.
"""
return False
"""
Return whether an argument begins with one of the prefixes.
"""
return
"""
Find the corresponding floating specification that matches a short
argument.
# Parameters:
- `form`:
The string to match against. It should be stripped of any prefix it
was found with.
# Returns:
The floating specification that matched, or `self.arg_unknown_floating`
if no match was found.
"""
return
return
"""
Find the corresponding floating specification that matches a long
argument.
# Parameters:
- `form`:
The string to match against. It should be stripped of any prefix it
was found with.
# Returns:
The floating specification that matched, or `self.arg_unknonw_floating`
if no match was found.
"""
return
return
"""
Get the list of arguments that can be consumed by a specification.
"""
break
yield
"""
Parse a special argument.
This method is meant to be overridden by subclasses that wish to use it
for parsing specially-subclassed versions of `Arg_spec`. It is only ever
called if `is_arg_special` returns `True`.
# Parameters
- `arg`:
The special argument to parse.
- `consumable_strs`:
The list of possible arguments that haven't been parsed yet but may
be consumed by any special specifications used.
# Yields:
A parsed argument.
# Raise:
- `NotImplementedError` if called without being overridden. Overriding
methods should *not* raise this or any other exception unless it is
to serve a similar purpose and require overriding by further
subclassing.
"""
"""
Parse one or more short floating arguments.
# Parameters
- `arg`:
The argument to parse. It should still have the prefix at the
beginning. It may contain multiple characters corresponding to
multiple short arguments to be parsed.
- `args`:
The current list of remaining arguments that haven't been parsed
yet.
# Yields:
A parsed argument.
"""
=
# TODO: return some error or something (this should really be
# impossible)
...
return
=
=
=
=
yield
=
"""
Parse a long floating argument.
# Parameters
- `arg`:
The argument to parse. It should still have the prefix at the
beginning.
- `args`:
The current list of remaining arguments that haven't been parsed
yet.
# Yields:
A parsed argument.
"""
=
# TODO: return some error or something (this should really be
# impossible)
...
return
=
=
=
yield
"""
Parse a positional argument.
# Parameters
- `spec`:
The positional argument specification to parse for.
- `args`:
The list of remaining arguments that haven't been parsed yet.
# Yields:
A parsed argument.
"""
=
=
yield
"""
Parse arguments according to the provided specification during
initialization.
# Parameters:
- `args`:
The list of arguments to parse.
# Yields:
A parsed argument.
"""
=
=
=
yield
=
=
:
=
:
=
:
=
= +
=
=
=
yield