import sys import os.path import argparse class Settings(): pass def get_path(path): return def check_path(path, force=False): return def is_valid_file(parser, arg): """Helper function for argparse to check if a file exists""" if not os.path.isfile(arg): parser.error('The file %s does not exist!' % arg) else: #return open(arg, 'r') return arg def is_valid_dir(parser, arg): """Helper function for argparse to check if a directory exists""" if not os.path.isdir(os.path.expanduser(arg)): parser.error('The directory %s does not exist!' % arg) else: return os.path.expanduser(arg) def main(): """Main function, argparse demonstration""" parser = argparse.ArgumentParser(description='Example method to demonstrate some ' 'some of the capabilities of argsparse') parser.add_argument('-c', '--config', nargs=1, metavar='config_file', dest='config', required=True, type=lambda x: is_valid_file(parser, x), help='Required: Specify a config file') parser.add_argument('-o', '--output', nargs=1, metavar='output_directory', type=lambda x: is_valid_dir(parser, x), help='Optional: Custom output directory') parser.add_argument('-l', '--list', nargs='?', const=True, help='List the amount of existing entries in files' 'in the output directory and exit; if "all" is specified' 'as an optional argument, the amount of entries will be listed') parser.add_argument('-e', '--example-config', nargs='?', const='example.config', help='Export an example config file and exit. ' 'If no file is specified, the output will be written to "example.config"') parser.add_argument('-n', '--number', type=int, nargs=1, metavar='integer', help='Number of entries') parser.add_argument('-q', '--queue', type=str, nargs=1, metavar='queue', help='The used queue system') parser.add_argument('-f', '--force', action='store_true', help='Force creation of directories if they do not exist' 'or overwrite existing files') parser.add_argument('-v', '--verbose', action='store_true', help='Print additional output') args = parser.parse_args() verbose = args.verbose force = args.force if args.example_config: print('[INFO] Write example config to file "%s"' % args.example_config) sys.exit(0) config = args.config settings = None if verbose: print('Use config file %s' % config) with open(config, 'r') as conf: settings = settings.read_config(conf) if args.list: if verbose and args.list == 'all': print('Trying to determine the total amount file entries') if args.output: settings.set('OUTPUT_PATH', get_path(args.output[0])) list_file_amount(settings, args.list == 'all') sys.exit(0) if args.queue: if verbose: print('Use the queue', args.queue[0]) settings.set('QUEUE', args.queue[0]) if args.number: if verbose: print('%d entries specified', args.number[0]) settings.set('NUMBER', args.number[0]) if verbose: print('The following settings will be used:') settings.print() if args.output: if not check_path(args.output[0], force): sys.exit('The output directory %s cannot be used' % args.output[0]) output = get_path(args.output[0]) print('Setting custom output directory: %s' % output)