| tparse options sensibly - slidergrid - grid of elastic sliders on a frictional … | |
| git clone git://src.adamsgaard.dk/slidergrid | |
| Log | |
| Files | |
| Refs | |
| README | |
| LICENSE | |
| --- | |
| commit 1a2317b2dc0f603bfe2ace1f0d5d93b40efd6a57 | |
| parent 9a7c3f858d084821a9aba2343c74f8607fbffed2 | |
| Author: Anders Damsgaard <[email protected]> | |
| Date: Thu, 17 Mar 2016 13:54:37 -0700 | |
| parse options sensibly | |
| Diffstat: | |
| M postprocessing.py | 66 +++++++++++++++++++++--------… | |
| 1 file changed, 46 insertions(+), 20 deletions(-) | |
| --- | |
| diff --git a/postprocessing.py b/postprocessing.py | |
| t@@ -1,22 +1,25 @@ | |
| #!/usr/bin/env python | |
| import sys | |
| +import getopt | |
| import os | |
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| VERSION = '0.1-beta' | |
| +SCRIPTNAME = sys.argv[0] | |
| -def print_usage(argv0): | |
| - print('usage: ' + argv0 + ' [OPTIONS] <FOLDER>') | |
| +def print_usage(): | |
| + print('usage: ' + SCRIPTNAME + ' [OPTIONS] <FOLDER1> [FOLDER2, ...]') | |
| print('where FOLDER is an output folder placed in this directory') | |
| print('options:') | |
| - print(' -h, --help \tshow this information') | |
| - print(' -v, --version \tshow version information') | |
| + print(' -h, --help show this information') | |
| + print(' -v, --version show version information') | |
| + print(' -ps, --plot-sliders plot slider positions') | |
| -def print_version(argv0): | |
| - print(argv0 + ' ' + VERSION) | |
| +def print_version(): | |
| + print(SCRIPTNAME + ' ' + VERSION) | |
| print('author: Anders Damsgaard, [email protected]') | |
| print('web: https://github.com/anders-dc/slidergrid') | |
| print('Licensed under the GNU Public License v3, see LICENSE for details') | |
| t@@ -44,22 +47,45 @@ class sgvis: | |
| self.read_sliders(filename) | |
| self.plot_sliders() | |
| -if __name__ == '__main__': | |
| - if len(sys.argv) < 2: | |
| - print_usage(sys.argv[0]) | |
| - exit() | |
| - for arg in sys.argv: | |
| +def iterate_over_folders(folders, plot_sliders): | |
| + if len(folders) < 1: | |
| + print_usage() | |
| + sys.exit(1) | |
| + | |
| + for folder in folders: | |
| + vis = sgvis(folder) | |
| + | |
| + if plot_sliders: | |
| + vis.plot_all_sliders() | |
| + | |
| - if sys.argv[1] == '-h' or sys.argv[1] == '--help': | |
| - print_usage(sys.argv[0]) | |
| - exit() | |
| +def main(argv): | |
| + folders = [] | |
| + plot_sliders = False | |
| + try: | |
| + opts, args = getopt.getopt(argv, 'hvs', | |
| + ['help', 'version', 'plot-sliders']) | |
| + except getopt.GetoptError: | |
| + print_usage() | |
| + sys.exit(2) | |
| - if sys.argv[1] == '-v' or sys.argv[1] == '--version': | |
| - print_version(sys.argv[0]) | |
| - exit() | |
| + for opt, arg in opts: | |
| + if opt in ('-h', '--help'): | |
| + print_usage() | |
| + sys.exit(0) | |
| - sgvis = sgvis(sys.argv[1]) | |
| + elif opt in ('-v', '--version'): | |
| + print_version() | |
| + sys.exit(0) | |
| - if sys.argv[1] == '-v' or sys.argv[1] == '--version': | |
| - sgvis.plot_all_sliders() | |
| + elif opt in ('-s', '--plot-sliders'): | |
| + plot_sliders = True | |
| + | |
| + else: | |
| + folders.append(arg) | |
| + | |
| + iterate_over_folders(folders, plot_sliders) | |
| + | |
| +if __name__ == '__main__': | |
| + main(sys.argv[1:]) |