Unified output processing options under --pretty.

* --pretty=none instead of --ugly
* --pretty=all instead of --pretty
* --pretty=colors instead of --colors
* --pretty=format instead of --format
This commit is contained in:
Jakub Roztocil committed 2012-08-17 21:15:37 +02:00
1 parent 86256af1df
commit bf03937f06
4 files changed
+55 -58

No files matched your search

+10 -23
View File
@@ -14,8 +14,7 @@ from .input import (Parser, AuthCredentialsArgType, KeyValueArgType,
SEP_PROXY, SEP_CREDENTIALS, SEP_GROUP_ITEMS,
OUT_REQ_HEAD, OUT_REQ_BODY, OUT_RESP_HEAD,
OUT_RESP_BODY, OUTPUT_OPTIONS,
PRETTY_STDOUT_TTY_ONLY, PRETTY_ALL,
PRETTY_FORMAT, PRETTY_COLORS)
PRETTY_MAP, PRETTY_STDOUT_TTY_ONLY)
def _(text):
@@ -69,29 +68,17 @@ parser.add_argument(
)
)
prettify = parser.add_mutually_exclusive_group(required=False)
prettify.add_argument(
'--pretty', dest='prettify', action='store_const', const=PRETTY_ALL,
default=PRETTY_STDOUT_TTY_ONLY,
parser.add_argument(
'--pretty', dest='prettify', default=PRETTY_STDOUT_TTY_ONLY,
choices=sorted(PRETTY_MAP.keys()),
help=_('''
Apply both colors and formatting. Default for terminal output.
Controls output processing. The value can be "none" to not prettify
the output (default for redirected output), "all" to apply both colors
and formatting
(default for terminal output), "colors", or "format".
''')
)
prettify.add_argument(
'--colors', dest='prettify', action='store_const', const=PRETTY_COLORS,
help=_('''Apply colors to the output.''')
)
prettify.add_argument(
'--format', dest='prettify', action='store_const', const=PRETTY_FORMAT,
help=_('''Apply formatting to the output.''')
)
prettify.add_argument(
'--ugly', '-u', dest='prettify', action='store_false',
help=_('''
Disables output processing.
Default for redirected output.
''')
)
output_options = parser.add_mutually_exclusive_group(required=False)
output_options.add_argument('--print', '-p', dest='output_options',
@@ -184,7 +171,7 @@ parser.add_argument(
# ``requests.request`` keyword arguments.
parser.add_argument(
'--auth', '-a', metavar='USER:PASS',
'--auth', '-a', metavar='USER[:PASS]',
type=AuthCredentialsArgType(SEP_CREDENTIALS),
help=_('''
username:password.
+15 -7
View File
@@ -67,9 +67,12 @@ OUTPUT_OPTIONS = frozenset([
])
# Pretty
PRETTY_ALL = ['format', 'colors']
PRETTY_FORMAT = ['format']
PRETTY_COLORS = ['colors']
PRETTY_MAP = {
'all': ['format', 'colors'],
'colors': ['colors'],
'format': ['format'],
'none': []
}
PRETTY_STDOUT_TTY_ONLY = object()
@@ -114,6 +117,7 @@ class Parser(argparse.ArgumentParser):
env.stdout_isatty = False
self._process_output_options(args, env)
self._process_pretty_options(args, env)
self._guess_method(args, env)
self._parse_items(args)
@@ -128,10 +132,6 @@ class Parser(argparse.ArgumentParser):
# Stdin already read (if not a tty) so it's save to prompt.
args.auth.prompt_password(urlparse(args.url).netloc)
if args.prettify == PRETTY_STDOUT_TTY_ONLY:
args.prettify = PRETTY_ALL if env.stdout_isatty else []
elif args.prettify and env.is_windows:
self.error('Only terminal output can be prettified on Windows.')
return args
@@ -246,6 +246,14 @@ class Parser(argparse.ArgumentParser):
if unknown:
self.error('Unknown output options: %s' % ','.join(unknown))
def _process_pretty_options(self, args, env):
if args.prettify == PRETTY_STDOUT_TTY_ONLY:
args.prettify = PRETTY_MAP['all' if env.stdout_isatty else 'none']
elif args.prettify and env.is_windows:
self.error('Only terminal output can be prettified on Windows.')
else:
args.prettify = PRETTY_MAP[args.prettify]
class ParseError(Exception):
pass