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:
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:

str

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:

str

Returns:

What you would need to run on the command line to reproduce what was run before.

A Utility Function

reverse_argparse.reverse_argparse.quote_arg_if_necessary(arg)[source]

Quote an argument, if necessary.

If a command line argument has any spaces in it, surround it in single quotes. If no quotes are necessary, don’t change the argument.

Parameters:

arg (str) – The command line argument.

Return type:

str

Returns:

The (possibly) quoted argument.

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 a argparse.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.

Type:

List[str]

_indent

The number of spaces with which to indent subsequent lines when pretty-printing the effective command line invocation.

Type:

int

_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:

List[argparse.ArgumentParser]

_unparsed

A list in which the elements indicate whether the corresponding parser in parsers has been unparsed.

Type:

List[bool]

Initialize the object.

Parameters:
_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.

Parameters:

arg (str) – The command line argument to be appended.

Return type:

None

_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.

Parameters:

args (List[str]) – The command line arguments to be appended.

Return type:

None

_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.

Parameters:

args (List[List[str]]) – The command line arguments to be appended.

Return type:

None

_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.

Parameters:

action (Action) – The command line argument in question.

Return type:

bool

Returns:

True if the argument should be omitted; False otherwise.

_get_long_option_strings(option_strings)[source]

Get the long options from a list of options strings.

Parameters:

option_strings (Sequence[str]) – The list of options strings.

Return type:

List[str]

Returns:

A list containing only the long options (e.g., "--foo"), and not the short ones (e.g., "-f"). Note that the list will be empty if there are no long options.

_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. If prefer_short is True, search the short options first, and fall back to the long ones if necessary.

Parameters:
  • action (Action) – The argparse.Action in question.

  • prefer_short (bool) – Whether to prefer the short options over the long ones.

Return type:

str

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.

Parameters:

option_strings (Sequence[str]) – The list of options strings.

Return type:

List[str]

Returns:

A list containing only the short options (e.g., "-f"), and not the short ones (e.g., "--foo"). Note that the list will be empty if there are no short options.

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) – The argparse.Action to unparse.

Raises:

NotImplementedError – If there is not currently an implementation for unparsing the given action.

Return type:

None

_unparse_append_action(action)[source]

Generate the list of arguments for an append action.

Parameters:

action (Action) – The _AppendAction in question.

Return type:

None

_unparse_append_const_action(action)[source]

Generate the argument for an append_const action.

Parameters:

action (Action) – The _AppendConstAction in question.

Return type:

None

_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:

None

_unparse_boolean_optional_action(action)[source]

Generate the list of arguments for a BooleanOptionalAction.

Parameters:

action (Action) – The BooleanOptionalAction in question.

Return type:

None

_unparse_count_action(action)[source]

Generate the list of arguments for a count action.

Parameters:

action (Action) – The _CountAction in question.

Return type:

None

_unparse_extend_action(action)[source]

Generate the list of arguments for an extend action.

Parameters:

action (Action) – The _ExtendAction in question.

Return type:

None

_unparse_store_action(action)[source]

Generate the list of arguments for a store action.

Parameters:

action (Action) – The _StoreAction in question.

Return type:

None

_unparse_store_const_action(action)[source]

Generate the argument for a store_const action.

Parameters:

action (Action) – The _StoreConstAction in question.

Return type:

None

_unparse_store_false_action(action)[source]

Generate the argument for a store_false action.

Parameters:

action (Action) – The _StoreFalseAction in question.

Return type:

None

_unparse_store_true_action(action)[source]

Generate the argument for a store_true action.

Parameters:

action (Action) – The _StoreTrueAction in question.

Return type:

None

_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:

None