131 lines
4.5 KiB
Python
131 lines
4.5 KiB
Python
import argparse
|
|
import sys
|
|
import os
|
|
|
|
import ConsoleUtils
|
|
|
|
# Some information about the program...
|
|
_prog = 'Program name'
|
|
_usage = '[main-file.py] -a,--argument ARG [options...]'
|
|
_descript = 'Brief description.'
|
|
_version = '0.0.1'
|
|
_author = 'John Doe <jdoe@company.com>'
|
|
_date = 'YYYY/MM/DD' # datetime.datetime.now().strftime('%Y.%m.%d-%H:%M:%S')
|
|
|
|
# Defaults for arguments/options which are accessed outside of the commandline
|
|
_def_arg_2 = 'Some default value'
|
|
|
|
|
|
# This is the main function of the program.
|
|
def main():
|
|
print('Required argument = %s' % args.argument1)
|
|
print('Optional argument = %s' % args.argument2)
|
|
|
|
|
|
# This is where we do a check of the arguments If necessary (perhaps you only take certain inputs for an argument).
|
|
def _check_args():
|
|
# if this script isn't being run as a program...
|
|
if __name__ != '__main__':
|
|
print('No arguments to check, exiting...')
|
|
exit(0)
|
|
|
|
fatal_errors = False
|
|
# Do whatever checks are needed on the optional arguments here.
|
|
if args.argument2 is not None:
|
|
print('"%s" is a valid argument!' % args.argument2)
|
|
# If they weren't valid, let the user know you're going with the default value. We print an error for an optional
|
|
# argument being bad.
|
|
else:
|
|
print('Warning: "%s" is not valid\n'
|
|
' OK Using the default of "%s"%s' % (args.argument2, _def_arg_2, os.linesep))
|
|
|
|
# Do the checks for the required arguments second, this makes more sense reading the output.
|
|
if not args.argument1 == 'bad value':
|
|
print('"%s" is a valid argument!' % args.argument1)
|
|
# We print an error for a required argument being bad.
|
|
else:
|
|
print('Error: "%s" is not a valid argument!' % args.argument1)
|
|
fatal_errors = True
|
|
|
|
# We only exit if a required argument was bad, we can handle optional arguments.
|
|
if fatal_errors:
|
|
_parser.print_help()
|
|
print('Exiting...')
|
|
exit(0)
|
|
|
|
|
|
# Prints the argument values passed in.
|
|
def _show_args():
|
|
# print the arguments with a brief summation of what they mean.
|
|
print('Value of Argument 1: %s' % args.argument)
|
|
print('Value of Argument 2: %s' % args.option)
|
|
|
|
|
|
# Set up arguments here; more groups can be made if required.
|
|
def _make_args():
|
|
# Create the arguments here...
|
|
_parser.add_argument('-a', '--argument',)
|
|
|
|
# options here
|
|
_parser.add_argument('-o', '--option', default=_def_arg_2)
|
|
|
|
# misc here
|
|
_parser.add_argument('-h', '--help', action=ConsoleUtils.CustomPrintAction, print_fn=_print_help)
|
|
_parser.add_argument('-v', '--version', action=ConsoleUtils.CustomPrintAction, print_fn=_print_version)
|
|
|
|
|
|
# Prints the help information (will need editing).
|
|
def _print_help():
|
|
print('')
|
|
print(_prog)
|
|
print(_descript)
|
|
print('Usage: %s' % _usage)
|
|
print('')
|
|
print('Arguments:')
|
|
print(' -a, --argument ARG Some argument.')
|
|
print('')
|
|
print('Options:')
|
|
print(' -o, --option OPT Some option (default: %s).' % _def_arg_2)
|
|
print('')
|
|
print('Miscellaneous:')
|
|
print(' -h, --help Prints the help message.')
|
|
print(' -v, --version Prints the version info.')
|
|
print('')
|
|
print('Version:')
|
|
print(' Version: %s' % _version)
|
|
print(' Date: %s' % _date)
|
|
print('')
|
|
print('Author: %s' % _author)
|
|
print('')
|
|
|
|
|
|
# prints the version information (may not need editing)
|
|
def _print_version():
|
|
print('%s %s (%s)' % (_prog, _version, _date))
|
|
|
|
|
|
# What happens if this is run as an independent program
|
|
if __name__ == '__main__':
|
|
# Print a nice header
|
|
print(ConsoleUtils.get_header(_prog, _version, _date, _author))
|
|
# Define the variables which are only used if this is being run independently.
|
|
# The argument parser to use
|
|
_parser = argparse.ArgumentParser(prog=_prog, description=_descript, usage=_usage, add_help=False)
|
|
# Create the arguments...
|
|
_make_args()
|
|
|
|
# Get the arguments, we don't need to do a check for required arguments, since the ArgumentParser class does that
|
|
# for us already in the 'parse_args' method. We can do custom argument checks in our 'check_args' method.
|
|
args = _parser.parse_args()
|
|
# arguments are referenced by args.[long_name]
|
|
# ex: the option "--argument" is accessed with args.argument
|
|
|
|
# Set sys.stdout to a custom printer
|
|
_printer = ConsoleUtils.SLPrinter(prog_name=_prog)
|
|
sys.stdout = _printer
|
|
# Calling sys.stdout = _printer.old_stdout from anywhere in the program will reset the std out to normal.
|
|
# Do an argument check.
|
|
_check_args()
|
|
# Now we can run...
|
|
main()
|