Set all defaults for source in arg parser.

This commit is contained in:
Chris Berkhout 2021-08-02 16:25:30 +02:00
parent 16d0405725
commit 338acf2970

View file

@ -42,21 +42,16 @@ def cli(argv=sys.argv, output_file=sys.stdout):
elif args.command == "fetch": elif args.command == "fetch":
source = sources.by_id[args.source] source = sources.by_id[args.source]
output = outputs.by_type[args.output] output = outputs.by_type[args.output]
if args.start: if args.end < args.start:
start = args.start
else:
start = source.start()
logging.info(f"Using the source default start date of {start}.")
if args.end < start:
logging.critical( logging.critical(
f"The end date '{args.end}' preceeds the start date '{start}'!" f"The end date '{args.end}' preceeds the start date '{args.start}'!"
) )
sys.exit(1) sys.exit(1)
series = Series( series = Series(
base=source.normalizesymbol(args.pair[0]), base=source.normalizesymbol(args.pair[0]),
quote=source.normalizesymbol(args.pair[1]), quote=source.normalizesymbol(args.pair[1]),
type=args.type or (source.types() + ["(none)"])[0], type=args.type,
start=start, start=args.start,
end=args.end, end=args.end,
) )
if series.type not in source.types(): if series.type not in source.types():
@ -116,6 +111,15 @@ def build_parser():
def formatter(prog): def formatter(prog):
return argparse.HelpFormatter(prog, max_help_position=50) return argparse.HelpFormatter(prog, max_help_position=50)
class SetSourceDefaults(argparse.Action):
def __call__(self, parser, namespace, value, option_string=None):
source = sources.by_id[value]
setattr(namespace, self.dest, value)
if getattr(namespace, "type") is None:
setattr(namespace, "type", source.types()[0])
if getattr(namespace, "start") is None:
setattr(namespace, "start", source.start())
default_fmt = Format() default_fmt = Format()
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
prog="pricehist", prog="pricehist",
@ -206,6 +210,7 @@ def build_parser():
metavar="SOURCE", metavar="SOURCE",
type=str, type=str,
choices=sources.by_id.keys(), choices=sources.by_id.keys(),
action=SetSourceDefaults,
help="the source identifier", help="the source identifier",
) )
fetch_parser.add_argument( fetch_parser.add_argument(
@ -226,7 +231,7 @@ def build_parser():
dest="type", dest="type",
metavar="TYPE", metavar="TYPE",
type=str, type=str,
help="price type, e.g. close", help="price type, e.g. close (default: first for source)",
) )
fetch_start_group = fetch_parser.add_mutually_exclusive_group(required=False) fetch_start_group = fetch_parser.add_mutually_exclusive_group(required=False)
fetch_start_group.add_argument( fetch_start_group.add_argument(