Skip to content

Instantly share code, notes, and snippets.

@Daveiano
Forked from opie4624/commandline.py
Created May 1, 2023 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Daveiano/9ad7fbf88acdc960aa18a86428b72521 to your computer and use it in GitHub Desktop.
Save Daveiano/9ad7fbf88acdc960aa18a86428b72521 to your computer and use it in GitHub Desktop.
Base Python Command Line template
#!/usr/bin/env python
#
# import modules used here -- sys is a very standard one
import sys, argparse, logging
# Gather our code in a main() function
def main(args, loglevel):
logging.basicConfig(format="%(levelname)s: %(message)s", level=loglevel)
# TODO Replace this with your actual code.
print "Hello there."
logging.info("You passed an argument.")
logging.debug("Your Argument: %s" % args.argument)
# Standard boilerplate to call the main() function to begin
# the program.
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description = "Does a thing to some stuff.",
epilog = "As an alternative to the commandline, params can be placed in a file, one per line, and specified on the commandline like '%(prog)s @params.conf'.",
fromfile_prefix_chars = '@' )
# TODO Specify your real parameters here.
parser.add_argument(
"argument",
help = "pass ARG to the program",
metavar = "ARG")
parser.add_argument(
"-v",
"--verbose",
help="increase output verbosity",
action="store_true")
args = parser.parse_args()
# Setup logging
if args.verbose:
loglevel = logging.DEBUG
else:
loglevel = logging.INFO
main(args, loglevel)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment