def preprocess_argv(): # Remove script from argv argv = sys.argv[1:] if len(argv): command_abbreviations = { 'l': 'list', 'u': 'update', 'n': 'new', 'd': 'delete', 'p': 'periods' } if argv[0] in command_abbreviations: # Expand command abbreviation argv[0] = command_abbreviations[argv[0]] elif argv[0][0:1] == '+': # "+<issue>" is shorthand for "new <issue>" argv = ['new', argv[0][1:]] + argv[1:] elif len(argv) == 1 and helpers.resolve_period_abbreviation(argv[0]): # If time period given, not command, use as basis for list command argv = ['list', argv[0]] else: # Default to "list" command argv = ['list'] return argv
def validate_args(parser, args, config): # Normalize and validate period if 'period' in args and args.period: args.period = helpers.resolve_period_abbreviation(args.period) if not args.period: parser.error('Invalid period.') # Normalize and validate project/entry ID if 'id' in args and args.id: if args.command == 'new': # Allow use of preset comments and/or hours default_comments = helpers.template_field(args.id, 'comments', config['projects']) default_hours = helpers.template_field(args.id, 'hours', config['projects']) if default_comments and not args.comments: args.comments = default_comments if default_hours and not args.hours: args.hours = default_hours # Resolve preset name to ID args.id = helpers.resolve_project_alias(args.id, config['projects']) # Resolve dates, if set if 'date' in args and args.date: args.date = resolve_and_validate_date_value(args.date, parser) if 'start' in args and args.start: args.start = resolve_and_validate_date_value(args.start, parser) if 'end' in args and args.end: args.end = resolve_and_validate_date_value(args.end, parser) # Sanity-check hours, if set if 'hours' in args and args.hours: try: float(args.hours) except ValueError: parser.error('Invalid hours value.') return args
def validate_args(parser, args, config, activities): # Normalize and validate period if 'period' in args and args.period: args.period = helpers.resolve_period_abbreviation(args.period) if not args.period: parser.error('Invalid period.') # Normalize and validate issue/entry ID if 'id' in args and args.id: if args.command == 'new': default_comments = helpers.template_field(args.id, 'comments', config['issues']) default_hours = helpers.template_field(args.id, 'hours', config['issues']) default_activity = helpers.template_field(args.id, 'activity', config['issues']) if default_comments and not args.comments: args.comments = default_comments if default_hours and not args.hours: args.hours = default_hours if default_activity and not args.activity: args.activity = default_activity args.id = helpers.resolve_issue_alias(args.id, config['issues']) if not str(args.id).isdigit(): parser.error('Invalid ID.') if 'activity' in args and args.activity: args.activity = helpers.resolve_activity_alias(args.activity, config['activities']) if args.activity not in activities: parser.error('Invalid activity.') return args