absl.flags.argparse_flags module

This module provides argparse integration with absl.flags.

argparse_flags.ArgumentParser is a drop-in replacement for argparse.ArgumentParser. It takes care of collecting and defining absl flags in argparse.

Here is a simple example:

# Assume the following absl.flags is defined in another module: # # from absl import flags # flags.DEFINE_string(‘echo’, None, ‘The echo message.’) # parser = argparse_flags.ArgumentParser(

description=’A demo of absl.flags and argparse integration.’)

parser.add_argument(’–header’, help=’Header message to print.’)

# The parser will also accept the absl flag –echo. # The header value is available as args.header just like a regular # argparse flag. The absl flag –echo continues to be available via # absl.flags.FLAGS if you want to access it. args = parser.parse_args()

# Example usages: # ./program –echo=’A message.’ –header=’A header’ # ./program –header ‘A header’ –echo ‘A message.’

Here is another example demonstrates subparsers:

parser = argparse_flags.ArgumentParser(description=’A subcommands demo.’) parser.add_argument(’–header’, help=’The header message to print.’)

subparsers = parser.add_subparsers(help=’The command to execute.’)

roll_dice_parser = subparsers.add_parser(

‘roll_dice’, help=’Roll a dice.’, # By default, absl flags can also be specified after the sub-command. # To only allow them before sub-command, pass # inherited_absl_flags=None. inherited_absl_flags=None)

roll_dice_parser.add_argument(’–num_faces’, type=int, default=6) roll_dice_parser.set_defaults(command=roll_dice)

shuffle_parser = subparsers.add_parser(‘shuffle’, help=’Shuffle inputs.’) shuffle_parser.add_argument(

‘inputs’, metavar=’I’, nargs=’+’, help=’Inputs to shuffle.’)

shuffle_parser.set_defaults(command=shuffle)

args = parser.parse_args(argv[1:]) args.command(args)

# Example usages: # ./program –echo=’A message.’ roll_dice –num_faces=6 # ./program shuffle –echo=’A message.’ 1 2 3 4

There are several differences between absl.flags and argparse_flags:

  1. Flags defined with absl.flags are parsed differently when using the argparse parser. Notably:

    1. absl.flags allows both single-dash and double-dash for any flag, and doesn’t distinguish them; argparse_flags only allows double-dash for flag’s regular name, and single-dash for flag’s short_name.

    2. Boolean flags in absl.flags can be specified with –bool, –nobool, as well as –bool=true/false (though not recommended); in argparse_flags, it only allows –bool, –nobool.

  2. Help related flag differences: 1) absl.flags does not define help flags, absl.app does that; argparse_flags

    defines help flags unless passed with add_help=False.

    1. absl.app supports –helpxml; argparse_flags does not.

    2. argparse_flags supports -h; absl.app does not.

class absl.flags.argparse_flags.ArgumentParser(**kwargs)[source]

Bases: ArgumentParser

Custom ArgumentParser class to support special absl flags.

parse_known_args(args=None, namespace=None)[source]