def __subparser_walker(self, command: str, parser: argparse.ArgumentParser): for action in parser._get_positional_actions(): if isinstance(action, _SubParsersAction): for (keyword, subparser) in action.choices.items(): subcommand = command + " " + keyword yield subcommand, subparser for s, p in self.__subparser_walker(subcommand, subparser): yield s, p
def __subparser_walker(self, command: str, parser: argparse.ArgumentParser): for action in parser._get_positional_actions(): if isinstance(action, _SubParsersAction): for (keyword, subparser) in action.choices.items(): subcommand = command + ' ' + keyword yield subcommand, subparser for s, p in self.__subparser_walker(subcommand, subparser): yield s, p
class Manager(object): ''' The manager of project construction and maintenance. Attributes ---------- args : list of str The arguments passed to this manager. parser : ArgumentParser The argument parser. ''' def __init__(self, args): ''' Constructor. Parameters ---------- args : list of str The args passed to this manager. ''' self.args = args self.parser = ArgumentParser(prog=os.path.basename(args[0])) self.init() self.add() def add_subcommand(self, name, subcommand): ''' Add a subcommand to the manager. Parameters ---------- name : str The name of the subcommand. subcommand : callable The function that implements the subcommand. Returns ------- ArgumentParser The subparser corresponding to the subcommand. ''' actions = self.parser.add_subparsers( ) if self.parser._subparsers is None else self.parser._get_positional_actions( )[0] subparser = actions.add_parser(name) subparser.set_defaults(subcommand=subcommand) return subparser def init(self): ''' Subcommand init, which initializes the project. ''' subcommand = self.add_subcommand('init', init) subcommand.add_argument('project', help='The project name') subcommand.add_argument( '-d', '--directory', help='The directory where to store the project.', default='.') subcommand.add_argument( '-a', '--authors', help='The authors, separated by commas, of the project.', default='Anonymous') subcommand.add_argument('-e', '--email', help='The contact email.', default='') subcommand.add_argument('-r', '--readme', help='The description of the project.', default='') subcommand.add_argument( '-l', '--license', help= "'T'/'t' for adding a GNU GPL v3.0 LICENSE file and 'F'/'f' for not.", default='t', choices=['T', 't', 'F', 'f']) subcommand.add_argument( '-g', '--gitignore', help="'T'/'t' for adding a gitignore file and 'F'/'f' for not.", default='t', choices=['T', 't', 'F', 'f']) def add(self): ''' Subcommand add, which adds an engine to this project. ''' subcommand = self.add_subcommand('add', add) subcommand.add_argument('engine', help='The engine to be added to the project.', choices=['tba', 'ed', 'vca', 'dmrg', 'fbfm']) subcommand.add_argument( '-s', '--system', help="'spin' for spin systems and 'fermi' for fermionic systems.", default='fermi', choices=['spin', 'fermi']) subcommand.add_argument( '-c', '--cluster', help="'single' for single cluster and 'multi' for multicluster.", default='single', choices=['single', 'multi']) def execute(self): ''' Execute the command line commands this manager manages. ''' namespace = self.parser.parse_args(self.args[1:] or ['-h']) namespace.subcommand( **{ key: value for key, value in vars(namespace).iteritems() if key != 'subcommand' }) @classmethod def has_project(cls): ''' Judge whether the current folder contains a project. ''' try: with open('README.md', 'r') as fin: name = fin.readline()[2:-1] assert name == os.path.basename(os.getcwd()) return True except: return False
class Manager(object): ''' The manager of project construction and maintenance. Attributes ---------- args : list of str The arguments passed to this manager. parser : ArgumentParser The argument parser. ''' def __init__(self,args): ''' Constructor. Parameters ---------- args : list of str The args passed to this manager. ''' self.args=args self.parser=ArgumentParser(prog=os.path.basename(args[0])) self.init() self.add() def add_subcommand(self,name,subcommand): ''' Add a subcommand to the manager. Parameters ---------- name : str The name of the subcommand. subcommand : callable The function that implements the subcommand. Returns ------- ArgumentParser The subparser corresponding to the subcommand. ''' actions=self.parser.add_subparsers() if self.parser._subparsers is None else self.parser._get_positional_actions()[0] subparser=actions.add_parser(name) subparser.set_defaults(subcommand=subcommand) return subparser def init(self): ''' Subcommand init, which initializes the project. ''' subcommand=self.add_subcommand('init',init) subcommand.add_argument('project',help='The project name') subcommand.add_argument('-d','--directory',help='The directory where to store the project.',default='.') subcommand.add_argument('-a','--authors',help='The authors, separated by commas, of the project.',default='Anonymous') subcommand.add_argument('-e','--email',help='The contact email.',default='') subcommand.add_argument('-r','--readme',help='The description of the project.',default='') subcommand.add_argument('-l','--license',help="'T'/'t' for adding a GNU GPL v3.0 LICENSE file and 'F'/'f' for not.",default='t',choices=['T','t','F','f']) subcommand.add_argument('-g','--gitignore',help="'T'/'t' for adding a gitignore file and 'F'/'f' for not.",default='t',choices=['T','t','F','f']) def add(self): ''' Subcommand add, which adds an engine to this project. ''' subcommand=self.add_subcommand('add',add) subcommand.add_argument('engine',help='The engine to be added to the project.',choices=['tba','ed','vca','vcacct','idmrg','fdmrg','fbfm']) subcommand.add_argument('-s','--system',help="'spin' for spin systems and 'fock' for fock systems.",default='fock',choices=['spin','fock']) def execute(self): ''' Execute the command line commands this manager manages. ''' namespace=self.parser.parse_args(self.args[1:] or ['-h']) namespace.subcommand(**{key:value for key,value in vars(namespace).items() if key!='subcommand'}) @classmethod def hasproject(cls): ''' Judge whether the current folder contains a project. ''' try: with open('README.md','r') as fin: name=fin.readline()[2:-1] assert name==os.path.basename(os.getcwd()) return True except: return False
def get_positional_actions(self, parser: argparse.ArgumentParser): return parser._get_positional_actions()