aboutsummaryrefslogtreecommitdiff
path: root/absl/flags/_flag.py
diff options
context:
space:
mode:
Diffstat (limited to 'absl/flags/_flag.py')
-rw-r--r--absl/flags/_flag.py85
1 files changed, 44 insertions, 41 deletions
diff --git a/absl/flags/_flag.py b/absl/flags/_flag.py
index d7ad944..124f137 100644
--- a/absl/flags/_flag.py
+++ b/absl/flags/_flag.py
@@ -31,42 +31,45 @@ from absl.flags import _helpers
class Flag(object):
"""Information about a command-line flag.
- 'Flag' objects define the following fields:
- .name - the name for this flag;
- .default - the default value for this flag;
- .default_unparsed - the unparsed default value for this flag.
- .default_as_str - default value as repr'd string, e.g., "'true'" (or None);
- .value - the most recent parsed value of this flag; set by parse();
- .help - a help string or None if no help is available;
- .short_name - the single letter alias for this flag (or None);
- .boolean - if 'true', this flag does not accept arguments;
- .present - true if this flag was parsed from command line flags;
- .parser - an ArgumentParser object;
- .serializer - an ArgumentSerializer object;
- .allow_override - the flag may be redefined without raising an error, and
- newly defined flag overrides the old one.
- .allow_override_cpp - use the flag from C++ if available; the flag
- definition is replaced by the C++ flag after init;
- .allow_hide_cpp - use the Python flag despite having a C++ flag with
- the same name (ignore the C++ flag);
- .using_default_value - the flag value has not been set by user;
- .allow_overwrite - the flag may be parsed more than once without raising
- an error, the last set value will be used;
- .allow_using_method_names - whether this flag can be defined even if it has
- a name that conflicts with a FlagValues method.
-
- The only public method of a 'Flag' object is parse(), but it is
- typically only called by a 'FlagValues' object. The parse() method is
- a thin wrapper around the 'ArgumentParser' parse() method. The parsed
- value is saved in .value, and the .present attribute is updated. If
- this flag was already present, an Error is raised.
-
- parse() is also called during __init__ to parse the default value and
- initialize the .value attribute. This enables other python modules to
- safely use flags even if the __main__ module neglects to parse the
- command line arguments. The .present attribute is cleared after
- __init__ parsing. If the default value is set to None, then the
- __init__ parsing step is skipped and the .value attribute is
+ Attributes:
+ name: the name for this flag
+ default: the default value for this flag
+ default_unparsed: the unparsed default value for this flag.
+ default_as_str: default value as repr'd string, e.g., "'true'"
+ (or None)
+ value: the most recent parsed value of this flag set by :meth:`parse`
+ help: a help string or None if no help is available
+ short_name: the single letter alias for this flag (or None)
+ boolean: if 'true', this flag does not accept arguments
+ present: true if this flag was parsed from command line flags
+ parser: an :class:`~absl.flags.ArgumentParser` object
+ serializer: an ArgumentSerializer object
+ allow_override: the flag may be redefined without raising an error,
+ and newly defined flag overrides the old one.
+ allow_override_cpp: use the flag from C++ if available the flag
+ definition is replaced by the C++ flag after init
+ allow_hide_cpp: use the Python flag despite having a C++ flag with
+ the same name (ignore the C++ flag)
+ using_default_value: the flag value has not been set by user
+ allow_overwrite: the flag may be parsed more than once without
+ raising an error, the last set value will be used
+ allow_using_method_names: whether this flag can be defined even if
+ it has a name that conflicts with a FlagValues method.
+ validators: list of the flag validators.
+
+ The only public method of a ``Flag`` object is :meth:`parse`, but it is
+ typically only called by a :class:`~absl.flags.FlagValues` object. The
+ :meth:`parse` method is a thin wrapper around the
+ :meth:`ArgumentParser.parse()<absl.flags.ArgumentParser.parse>` method. The
+ parsed value is saved in ``.value``, and the ``.present`` attribute is
+ updated. If this flag was already present, an Error is raised.
+
+ :meth:`parse` is also called during ``__init__`` to parse the default value
+ and initialize the ``.value`` attribute. This enables other python modules to
+ safely use flags even if the ``__main__`` module neglects to parse the
+ command line arguments. The ``.present`` attribute is cleared after
+ ``__init__`` parsing. If the default value is set to ``None``, then the
+ ``__init__`` parsing step is skipped and the ``.value`` attribute is
initialized to None.
Note: The default value is also presented to the user in the help
@@ -150,7 +153,7 @@ class Flag(object):
return repr('true')
else:
return repr('false')
- return repr(_helpers.str_or_unicode(value))
+ return repr(str(value))
def parse(self, argument):
"""Parses string and sets flag value.
@@ -307,13 +310,13 @@ class BooleanFlag(Flag):
"""Basic boolean flag.
Boolean flags do not take any arguments, and their value is either
- True (1) or False (0). The false value is specified on the command
- line by prepending the word 'no' to either the long or the short flag
+ ``True`` (1) or ``False`` (0). The false value is specified on the command
+ line by prepending the word ``'no'`` to either the long or the short flag
name.
For example, if a Boolean flag was created whose long name was
- 'update' and whose short name was 'x', then this flag could be
- explicitly unset through either --noupdate or --nox.
+ ``'update'`` and whose short name was ``'x'``, then this flag could be
+ explicitly unset through either ``--noupdate`` or ``--nox``.
"""
def __init__(self, name, default, help, short_name=None, **args): # pylint: disable=redefined-builtin