|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +""" |
| 5 | +Usage and parsing options. |
| 6 | +
|
| 7 | +Exit status: |
| 8 | +0 EXIT_SUCCESS success |
| 9 | +1 EXIT_USAGE failed parsing cli options |
| 10 | +2 EXIT_FAILED Execution error |
| 11 | +""" |
| 12 | + |
| 13 | +import os |
| 14 | +import sys |
| 15 | +import getopt |
| 16 | + |
| 17 | + |
| 18 | +# |
| 19 | +# Add basics about this program |
| 20 | +# |
| 21 | +PROGRAM = os.path.basename(sys.argv[0]) |
| 22 | +AUTHOR = "Your Name" |
| 23 | +EMAIL = "your@email" |
| 24 | +VERSION = "v1.0.0 (2017-06-15)" |
| 25 | + |
| 26 | +MSG_USAGE = """{program} - short description on what this command does. |
| 27 | +By {author} ({email}), version {version}. |
| 28 | +
|
| 29 | +Usage: |
| 30 | + {program} [options] <command> |
| 31 | +
|
| 32 | +Options: |
| 33 | + -h, --help Display this help message. |
| 34 | + -v, --version Print version and exit. |
| 35 | + --silent Decrease verbose output. |
| 36 | + --verbose Increase verbose output. |
| 37 | +""".format( |
| 38 | + program=PROGRAM, |
| 39 | + author=AUTHOR, |
| 40 | + email=EMAIL, |
| 41 | + version=VERSION |
| 42 | +) |
| 43 | + |
| 44 | +MSG_VERSION = "{version}".format(version=VERSION) |
| 45 | + |
| 46 | +MSG_HELP = "Use {program} --help to get usage.".format(program=PROGRAM) |
| 47 | + |
| 48 | + |
| 49 | +# |
| 50 | +# Global default settings affecting behaviour of script in several places |
| 51 | +# |
| 52 | +EXIT_SUCCESS = 0 |
| 53 | +EXIT_USAGE = 1 |
| 54 | +EXIT_FAILED = 2 |
| 55 | + |
| 56 | + |
| 57 | +class Usage(object): |
| 58 | + """ |
| 59 | + Parse CLI options and return as dict. |
| 60 | +
|
| 61 | + Present usage and version information. |
| 62 | + """ |
| 63 | + |
| 64 | + # pylint: disable=R0201 |
| 65 | + |
| 66 | + default_options = { |
| 67 | + "verbose": False, |
| 68 | + "silent": False, |
| 69 | + } |
| 70 | + |
| 71 | + options = {} |
| 72 | + |
| 73 | + def __init__(self): |
| 74 | + """Initiate.""" |
| 75 | + self.options = self.default_options.copy() |
| 76 | + |
| 77 | + def print_usage(self, exit_status=0): |
| 78 | + """Print usage information about the script and exit.""" |
| 79 | + print(MSG_USAGE) |
| 80 | + sys.exit(exit_status) |
| 81 | + |
| 82 | + def print_version(self): |
| 83 | + """Print version information and exit.""" |
| 84 | + print(MSG_VERSION) |
| 85 | + sys.exit() |
| 86 | + |
| 87 | + def parse_options(self, argv=None): |
| 88 | + """ |
| 89 | + Parse all command line options and arguments. |
| 90 | +
|
| 91 | + Return them as a dictionary. |
| 92 | + """ |
| 93 | + argv = argv if argv is not None else sys.argv[1:] |
| 94 | + try: |
| 95 | + opts, _ = getopt.getopt(argv, "hv", [ |
| 96 | + "help", |
| 97 | + "version", |
| 98 | + "silent", |
| 99 | + "verbose" |
| 100 | + ]) |
| 101 | + |
| 102 | + for opt, _ in opts: |
| 103 | + if opt in ("-h", "--help"): |
| 104 | + self.print_usage() |
| 105 | + |
| 106 | + elif opt in ("-v", "--version"): |
| 107 | + self.print_version() |
| 108 | + |
| 109 | + elif opt in "--silent": |
| 110 | + self.options["silent"] = True |
| 111 | + |
| 112 | + elif opt in "--verbose": |
| 113 | + self.options["verbose"] = True |
| 114 | + |
| 115 | + except getopt.GetoptError as err: |
| 116 | + print(err) |
| 117 | + print(MSG_HELP) |
| 118 | + sys.exit(EXIT_USAGE) |
| 119 | + |
| 120 | + return self.options |
0 commit comments