refactor argparse handling

This commit is contained in:
gcarq 2017-11-17 17:18:31 +01:00
parent 5be7be6189
commit b682262486
2 changed files with 19 additions and 10 deletions

View File

@ -2,6 +2,7 @@
import copy
import json
import logging
import sys
import time
import traceback
from datetime import datetime
@ -14,8 +15,9 @@ from jsonschema import validate
from freqtrade import __version__, exchange, persistence
from freqtrade.analyze import get_signal, SignalType
from freqtrade.misc import CONF_SCHEMA, State, get_state, update_state, parse_args, throttle
from freqtrade.misc import (
CONF_SCHEMA, State, get_state, update_state, build_arg_parser, throttle, FreqtradeException
FreqtradeException
)
from freqtrade.persistence import Trade
from freqtrade.rpc import telegram
@ -316,11 +318,8 @@ def main():
:return: None
"""
global _CONF
args = build_arg_parser().parse_args()
# Check if subcommand has been selected
if hasattr(args, 'func'):
args.func(args)
args = parse_args(sys.argv[1:])
if not args:
exit(0)
# Initialize logger

View File

@ -3,7 +3,7 @@ import enum
import logging
import os
import time
from typing import Any, Callable
from typing import Any, Callable, List
from wrapt import synchronized
@ -62,8 +62,11 @@ def throttle(func: Callable[..., Any], min_secs: float, *args, **kwargs) -> Any:
return result
def build_arg_parser() -> argparse.ArgumentParser:
""" Builds and returns an ArgumentParser instance """
def parse_args(args: List[str]):
"""
Parses given arguments and returns an argparse Namespace instance.
Returns None if a sub command has been selected and executed.
"""
parser = argparse.ArgumentParser(
description='Simple High Frequency Trading Bot for crypto currencies'
)
@ -94,7 +97,14 @@ def build_arg_parser() -> argparse.ArgumentParser:
action='store_true',
)
build_subcommands(parser)
return parser
parsed_args = parser.parse_args(args)
# No subcommand as been selected
if not hasattr(parsed_args, 'func'):
return parsed_args
parsed_args.func(parsed_args)
return None
def build_subcommands(parser: argparse.ArgumentParser) -> None: