Clyent is a python command line utiliy library for binstar, binstar-build and chalmers
Add some default arguments to the argument parser.
-
`--show-traceback/--hide-traceback
Show or hide the full traceback for chalmers user errors. Hdden tracebacks are meant for user errors only. The error messages will still be printed and the program will exit.
-
-v/--verbose
or-q/--quiet
Switch the amount of output to displat
-
--color/--no-color
Toggle color output
-
`-V/--version
Print version information and exit
This will add sub parsers from a python package e.g. for the directory structure:
package/__init__.py
package/commands/__init__.py
package/commands/command.py
One would do:
from package import commands
add_subparser_modules(parser, package)
Each command module must contain the funciton add_parser(subparsers)
which takes an argument subparsers
wich is the result of the function argparse.ArgumentParser.add_subparsers
The add_parser
method should call:
parser = subparsers.add_parser('some-name')
parser.set_defaults(main=your_main_function)
Set up loggers to print color errors
TODO: documnent more
This command will run your subcommand and capture the output
def main(args=None, exit=True):
parser = ArgumentParser(description=__doc__)
add_default_arguments(parser, version)
add_subparser_modules(parser, chalmers.commands)
args = parser.parse_args(args)
logfile = join(dirs.user_log_dir, 'chalmers.log')
setup_logging(logger, args.log_level, use_color=args.color,
show_tb=args.show_traceback, logfile=logfile)
run_command(args, exit=exit)
See the chalmers main script for more details.
>>> from clyent.logs.colors.printer import print_colors
>>> print_colors('Hey! {=This is an inline \nmessage!c:red,bold,underline} ...')
Hey! This is an inline
message
>>> print_colors('This is a format substitution {ok!c:green,bold} '
'Because the value contains unescaped characters', ok='{OK!}')
This is a format substitution OK! Because the value contains unescaped characters
>>> with color.blue:
print('This is a message within a color context')
This is a message within a color context
>>> print_colors(color.underline('hello'),
'is euqal to',
'{=hello!c:underline}')
hello is equal to hello