Thomas Perl  committed 71c75d6

Option parsing, auto-generation/-update of manpage

  • Participants
  • Parent commits df2194f
  • Branches master

Comments (0)

Files changed (5)

 include README
 recursive-include share *
 that need installing. The script is made so that it also can be run from
 source (or installed system-wide).
   1. Only set one value in (PACKAGE_NAME), rest is automatic
   2. Metadata only defined in one place (lib/<PACKAGE_NAME>/
   3. Known-good module documentation (PYTHONPATH=lib pydoc <PACKAGE_NAME>)
   4. Runnable from a source checkout or from system-wide installation
+  5. Standardized option parsing, including --version, --verbose and --help
+  6. Manual page can be auto-generated with help2man (sh
     PKG_DATA_DIR = os.path.join(prefix, bindir, 'share', PACKAGE_NAME)
-# Logging using the normal Python logging facility
+# Logging and option parsing
 import logging
-STDOUT_FMT = '%(created).2f [%(name)s] %(levelname)s: %(message)s'
-logging.basicConfig(format=STDOUT_FMT, level=logging.DEBUG)
-logger = logging.getLogger(PACKAGE_NAME)
+import optparse
 # You can import modules from the blubb package here
+import blubb
 from blubb import bla
-def main(args):
-    data_json = args[-1]
+def setup_logging(verbose=False):
+    STDOUT_FMT = '%(created).2f [%(name)s] %(levelname)s: %(message)s'
+    default_level = logging.DEBUG if verbose else logging.WARNING
+    logging.basicConfig(format=STDOUT_FMT, level=default_level)
+def parse_options(args):
+    long_description = blubb.__doc__.strip().split('\n\n', 1)[1]
+    usage_info = '\n\n'.join(('%prog [options]', long_description))
+    version_info = ' '.join(('%prog', blubb.__version__))
+    parser = optparse.OptionParser(usage=usage_info, version=version_info)
+    parser.add_option('-v', '--verbose',
+            action='store_true', dest='verbose', default=False,
+            help='print logging output on the console')
+    parser.add_option('-f', '--foo',
+            dest='foofile', metavar='FILE',
+            help='will do foo to FILE in addition to default things')
+    return parser.parse_args(args)
+def main(args, foofile=None, **kwargs):
+    logger = logging.getLogger(PACKAGE_NAME)
+    if foofile:
+        logger.error('Not implemented: Fooing of %s', options.foofile)
     if len(args) == 1:
-        logger.warn('No arguments, using %s as default', data_json)
         data_json = os.path.join(PKG_DATA_DIR, 'data.json')
+        logger.warn('No arguments, using %s as default', data_json)
+    else:
+        data_json = args[-1]
+'Using %s as input file', data_json)'Starting process with %s', data_json)
 if __name__ == '__main__':
-    main(sys.argv)
+    options, args = parse_options(sys.argv)
+    setup_logging(options.verbose)
+    main(args, **options.__dict__)

File share/man/man1/blubb.1

+.\" DO NOT MODIFY THIS FILE!  It was generated by help2man 1.40.12.
+.TH BLUBB "1" "March 2013" "blubb 1.0.0" "User Commands"
+blubb \- Single line overview what Blubb does
+.B blubb
+Longer description of what Blubb does. This can range from a single
+sentence to multiple paragraphs. Don't be afraid to go into detail.
+show program's version number and exit
+\fB\-h\fR, \fB\-\-help\fR
+show this help message and exit
+\fB\-v\fR, \fB\-\-verbose\fR
+print logging output on the console
+\fB\-f\fR FILE, \fB\-\-foo\fR=\fIFILE\fR
+will do foo to FILE in addition to default things


+# - Generate project manpage with help2man
+# You should run this script every time the description/options change
+if [ -z "$PYTHON" ]; then
+    PYTHON=python
+DESCRIPTION=$($PYTHON --description)
+mkdir -p $(dirname $MAN_PAGE)
+help2man --name="$DESCRIPTION" --no-info --output="$MAN_PAGE" "./$PROJECT_NAME"