annotate lemuriformes/cli.py @ 16:9b1bb9eee962

misc functions related to filesystem paths
author Jeff Hammel <k0scist@gmail.com>
date Sun, 10 Dec 2017 17:19:30 -0800
parents 59c91bfb6c06
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8
59c91bfb6c06 add basic CLI front-end
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 """
59c91bfb6c06 add basic CLI front-end
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2 methods and properties supporting
59c91bfb6c06 add basic CLI front-end
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3 command line interface
59c91bfb6c06 add basic CLI front-end
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 """
59c91bfb6c06 add basic CLI front-end
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5
59c91bfb6c06 add basic CLI front-end
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6
59c91bfb6c06 add basic CLI front-end
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7 import argparse
59c91bfb6c06 add basic CLI front-end
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8 import sys
59c91bfb6c06 add basic CLI front-end
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 from .cast import keyvalue
59c91bfb6c06 add basic CLI front-end
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10
59c91bfb6c06 add basic CLI front-end
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11
59c91bfb6c06 add basic CLI front-end
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12 class ConfigurationParser(argparse.ArgumentParser):
59c91bfb6c06 add basic CLI front-end
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13 """parse arguments with validation"""
59c91bfb6c06 add basic CLI front-end
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14
59c91bfb6c06 add basic CLI front-end
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15 def __init__(self, **kwargs):
59c91bfb6c06 add basic CLI front-end
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16 kwargs.setdefault('formatter_class', argparse.RawTextHelpFormatter)
59c91bfb6c06 add basic CLI front-end
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17 argparse.ArgumentParser.__init__(self, **kwargs)
59c91bfb6c06 add basic CLI front-end
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18 self.add_arguments()
59c91bfb6c06 add basic CLI front-end
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19 self.options = None
59c91bfb6c06 add basic CLI front-end
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20
59c91bfb6c06 add basic CLI front-end
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21 def add_arguments(self):
59c91bfb6c06 add basic CLI front-end
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22 """add arguments"""
59c91bfb6c06 add basic CLI front-end
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23 # Abstract Base Class
59c91bfb6c06 add basic CLI front-end
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24
59c91bfb6c06 add basic CLI front-end
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25 def parse_args(self, args=sys.argv[1:]):
59c91bfb6c06 add basic CLI front-end
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26 options = argparse.ArgumentParser.parse_args(self, args)
59c91bfb6c06 add basic CLI front-end
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 self.options = self.validate(options)
59c91bfb6c06 add basic CLI front-end
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28 return self.options
59c91bfb6c06 add basic CLI front-end
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29
59c91bfb6c06 add basic CLI front-end
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30 def validate(self, options):
59c91bfb6c06 add basic CLI front-end
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31 """validate options"""
59c91bfb6c06 add basic CLI front-end
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32 return options
59c91bfb6c06 add basic CLI front-end
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
33
59c91bfb6c06 add basic CLI front-end
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34 def keyvalue(self, arg):
59c91bfb6c06 add basic CLI front-end
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
35
59c91bfb6c06 add basic CLI front-end
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
36 try:
59c91bfb6c06 add basic CLI front-end
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
37 return keyvalue(arg)
59c91bfb6c06 add basic CLI front-end
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
38 except AssertionError as e:
59c91bfb6c06 add basic CLI front-end
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
39 self.error(str(e))