Reference
The reverse_argparse
module.
Defines the ReverseArgumentParser
class for unparsing arguments
that were already parsed via argparse
, and the
quote_arg_if_necessary()
helper function to surround any arguments
with spaces in them with quotes.
Public Interface
The ReverseArgumentParser Class
- ReverseArgumentParser.__init__(parser, namespace, indent=4)[source]
Initialize the object.
- Parameters:
parser (
ArgumentParser
) – Theargparse.ArgumentParser
used to construct the givennamespace
.namespace (
Namespace
) – The parsed arguments.indent (
int
) – How many spaces to use for each indentation level. (Seeget_pretty_command_line_invocation()
.)
- ReverseArgumentParser.get_effective_command_line_invocation()[source]
Get the effective command line invocation of a script.
This takes into account what was passed into the script on the command line, along with any default values, etc., such that there is no ambiguity in what exactly was run.
- Return type:
- Returns:
What you would need to run on the command line to reproduce what was run before.
- ReverseArgumentParser.get_pretty_command_line_invocation()[source]
Get a “pretty” version of the command that was run.
Similar to
get_effective_command_line_invocation()
, but generate a string ready for “pretty-printing”, with escaped newlines between each of the arguments, and appropriate indentation.- Return type:
- Returns:
What you would need to run on the command line to reproduce what was run before.
A Utility Function
Implementation Details
- class reverse_argparse.reverse_argparse.ReverseArgumentParser(parser, namespace, indent=4)[source]
Bases:
object
Argument parsing in reverse.
Whereas
argparse.ArgumentParser
is concerned with taking a bunch of command line arguments and parsing them into aargparse.Namespace
, this class is intended to do the opposite; that is, it’ll take the parsed arguments and create the effective command line invocation of the script that generated them. The motivation is to be able to tell users exactly what was used for all of the options, taking into consideration any defaults and other transformations that might’ve been applied in the midst of parsing, such that they’re able to reproduce a prior run of a script exactly.- _args
The list of arguments corresponding to each
argparse.Action
in the given parser, which is built up as the arguments are unparsed.
- _indent
The number of spaces with which to indent subsequent lines when pretty-printing the effective command line invocation.
- Type:
- _namespace
The parsed arguments.
- Type:
Namespace
- _parsers
The parser that was used to generate the parsed arguments. This is a
list
(conceptually a stack) to allow for sub-parsers, so the outer-most parser is the first item in the list, and sub-parsers are pushed onto and popped off of the stack as they are processed.- Type:
- _unparsed
A list in which the elements indicate whether the corresponding parser in
parsers
has been unparsed.
Initialize the object.
- Parameters:
parser (
ArgumentParser
) – Theargparse.ArgumentParser
used to construct the givennamespace
.namespace (
Namespace
) – The parsed arguments.indent (
int
) – How many spaces to use for each indentation level. (Seeget_pretty_command_line_invocation()
.)
- _append_arg(arg)[source]
Append to the list of unparsed arguments.
Given a command line argument corresponding to a particular action, append it to the list of arguments, taking into account indentation and the sub-parser nesting level.
- _append_list_of_args(args)[source]
Append to the list of unparsed arguments.
Given a list of command line arguments corresponding to a particular action, append them to the list of arguments, taking into account indentation and the sub-parser nesting level.
- _append_list_of_list_of_args(args)[source]
Append to the list of unparsed arguments.
Given a list of lists of command line arguments corresponding to a particular action, append them to the list of arguments, taking into account indentation and the sub-parser nesting level.
- _arg_is_default_and_help_is_suppressed(action)[source]
See if the argument should be skipped.
Determine whether the argument matches the default value and the corresponding help text has been suppressed. Such cases indicate that a parser author has hidden an argument from users, and the user hasn’t modified the value on the command line, so to match the author’s intent, we should omit the argument from the effective command line invocation.
- _get_long_option_strings(option_strings)[source]
Get the long options from a list of options strings.
- _get_option_string(action, *, prefer_short=False)[source]
Get the option string for the action.
Get the first of the long options corresponding to a given
argparse.Action
. If no long options are available, get the first of the short options. Ifprefer_short
isTrue
, search the short options first, and fall back to the long ones if necessary.- Parameters:
action (
Action
) – Theargparse.Action
in question.prefer_short (
bool
) – Whether to prefer the short options over the long ones.
- Return type:
- Returns:
The option string, or the empty string, if no options string exists (e.g., for positional arguments).
- _get_short_option_strings(option_strings)[source]
Get the short options from a list of options strings.
- property _indent_str: str
The indentation level.
- Returns:
A string of spaces corresponding to the indentation level.
- _unparse_action(action)[source]
Unparse a single action.
Generate the command line arguments associated with the given
action
, and append them to the list of arguments.- Parameters:
action (
Action
) – Theargparse.Action
to unparse.- Raises:
NotImplementedError – If there is not currently an implementation for unparsing the given action.
- Return type:
- _unparse_args()[source]
Unparse all the arguments.
Loop over the positional and then optional actions, generating the command line arguments associated with each, and appending them to the list of arguments.
- Return type:
- _unparse_boolean_optional_action(action)[source]
Generate the list of arguments for a
BooleanOptionalAction
.
- _unparse_sub_parsers_action(action)[source]
Generate the list of arguments for a subparser action.
This is done by:
looping over the commands and corresponding parsers in the given subparser action,
recursively unparsing the subparser, and
if the subparser wasn’t used to parse the command line arguments, removing it before continuing with the next subcommand-subparser pair.
- Parameters:
action (
Action
) – The_SubParsersAction
in question.- Raises:
RuntimeError – If a subparser action is somehow missing its dictionary of choices.
- Return type: