def usage(command_usage): """ Instead of the docopt way to show the usage information we provide an azurectl specific usage information. The usage data now always consists of * the generic call azurectl [global options] service <command> [<args>] * the command specific usage defined by the docopt string short form by default, long form with -h | --help * the global options """ with open(Defaults.project_file('cli.py'), 'r') as cli: program_code = cli.readlines() global_options = '\n' process_lines = False for line in program_code: if line.rstrip().startswith('global options'): process_lines = True if line.rstrip() == '"""': process_lines = False if process_lines: global_options += format(line) print('usage: azurectl [global options] service <command> [<args>]\n') print(format(command_usage).replace('usage:', ' ')) if 'global options' not in command_usage: print(format(global_options)) if not format(command_usage).startswith('usage:'): error_details = format(command_usage).splitlines()[0] print(error_details)
def __get_command_implementations(self, service): command_implementations = [] glob_match = Defaults.project_file('/') + 'commands/*.py' for source_file in glob.iglob(glob_match): with open(source_file, 'r') as source: for line in source: if re.search('usage: (.*)', line): command_path = os.path.basename( source_file ).replace('.py', '').split('_') if command_path[0] == service: command_implementations.append( ' '.join(command_path) ) break return command_implementations
def load_command(self): if self.loaded: return self.loaded command = self.get_command() service = self.get_servicename() if not command: raise AzureLoadCommandUndefined( 'No command specified for %s service' % service ) command_source_file = Defaults.project_file( 'commands/' + service + '_' + command + '.py' ) if not os.path.exists(command_source_file): prefix = 'usage:' for service_command in self.__get_command_implementations(service): print('%s azurectl %s' % (prefix, service_command)) prefix = ' ' raise SystemExit(1) self.loaded = importlib.import_module( 'azurectl.commands.' + service + '_' + command ) return self.loaded