diff options
author | Abseil Team <absl-team@google.com> | 2021-04-30 15:12:40 -0700 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2021-04-30 15:13:07 -0700 |
commit | 1b71112b8aa319e2a09dc90d51eefe26e921d0ce (patch) | |
tree | f64f29c63ff9d6a9a604b170dc0edf9702856548 /absl/app.py | |
parent | 5206702470497dd371ae75427ab5e8091652ecbb (diff) | |
download | absl-py-1b71112b8aa319e2a09dc90d51eefe26e921d0ce.tar.gz |
Shows all missing required flags in error message, instead of just the first missing required flag.
Given these required flags of float values with default value of None:
flags.mark_flags_as_required(["x", "y", "z"])
If none of the required flags are passed as arguments, the resulting error will only show the first missing required flag as an issue:
FATAL Flags parsing error: flag --x=None: Flag --x must have a value other than None.
The user will then add this required flag "x" and try again, but then get the error for "y" as a missing requirement. This loops until the user finally passes all the required flags as arguments.
Since we already know which flags are required, this changes the error message shows all missing required flags at once, so the user can make the necessary changes in a single pass with the error message showing this:
FATAL Flags parsing error:
flag --x=None: Flag --x must have a value other than None.
flag --y=None: Flag --y must have a value other than None.
flag --z=None: Flag --z must have a value other than None.
To achieve the formatting change, `app.parse_flags_with_usage` now puts the error message on a new line and adds indentation if the message is multi-line string.
PiperOrigin-RevId: 371414012
Change-Id: Id7456e9c293fb95d7b4551fd28441610af9b3030
Diffstat (limited to 'absl/app.py')
-rw-r--r-- | absl/app.py | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/absl/app.py b/absl/app.py index a6e9216..fbdfccd 100644 --- a/absl/app.py +++ b/absl/app.py @@ -34,6 +34,7 @@ import errno import os import pdb import sys +import textwrap import traceback from absl import command_name @@ -158,7 +159,13 @@ def parse_flags_with_usage(args): try: return FLAGS(args) except flags.Error as error: - sys.stderr.write('FATAL Flags parsing error: %s\n' % error) + message = str(error) + if '\n' in message: + final_message = 'FATAL Flags parsing error:\n%s\n' % textwrap.indent( + message, ' ') + else: + final_message = 'FATAL Flags parsing error: %s\n' % message + sys.stderr.write(final_message) sys.stderr.write('Pass --helpshort or --helpfull to see help on flags.\n') sys.exit(1) |