split common command line args parsing

A new function parse_args_common() that only parses
common command line options. The returned object can
be composed to parse more arguments.
As is done by parse_args().
This commit is contained in:
kryofly 2018-01-06 07:39:05 +01:00
parent 74a708b794
commit 47675943ee
3 changed files with 44 additions and 34 deletions

View File

@ -405,7 +405,8 @@ def main() -> None:
:return: None :return: None
""" """
global _CONF global _CONF
args = parse_args(sys.argv[1:]) args = parse_args(sys.argv[1:],
'Simple High Frequency Trading Bot for crypto currencies')
if not args: if not args:
exit(0) exit(0)

View File

@ -81,21 +81,12 @@ def throttle(func: Callable[..., Any], min_secs: float, *args, **kwargs) -> Any:
return result return result
def parse_args(args: List[str]): def parse_args_common(args: List[str], description: str):
""" """
Parses given arguments and returns an argparse Namespace instance. Parses given common arguments and returns them as a parsed object.
Returns None if a sub command has been selected and executed.
""" """
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description='Simple High Frequency Trading Bot for crypto currencies' description=description
)
parser.add_argument(
'-c', '--config',
help='specify configuration file (default: config.json)',
dest='config',
default='config.json',
type=str,
metavar='PATH',
) )
parser.add_argument( parser.add_argument(
'-v', '--verbose', '-v', '--verbose',
@ -110,6 +101,30 @@ def parse_args(args: List[str]):
action='version', action='version',
version='%(prog)s {}'.format(__version__), version='%(prog)s {}'.format(__version__),
) )
parser.add_argument(
'-c', '--config',
help='specify configuration file (default: config.json)',
dest='config',
default='config.json',
type=str,
metavar='PATH',
)
return parser
def parse_args(args: List[str], description: str):
"""
Parses given arguments and returns an argparse Namespace instance.
Returns None if a sub command has been selected and executed.
"""
parser = parse_args_common(args, description)
parser.add_argument(
'--dry-run-db',
help='Force dry run to use a local DB "tradesv3.dry_run.sqlite" instead of memory DB. Work only if dry_run is \
enabled.', # noqa
action='store_true',
dest='dry_run_db',
)
parser.add_argument( parser.add_argument(
'--dynamic-whitelist', '--dynamic-whitelist',
help='dynamically generate and update whitelist based on 24h BaseVolume (Default 20 currencies)', # noqa help='dynamically generate and update whitelist based on 24h BaseVolume (Default 20 currencies)', # noqa
@ -119,13 +134,7 @@ def parse_args(args: List[str]):
metavar='INT', metavar='INT',
nargs='?', nargs='?',
) )
parser.add_argument(
'--dry-run-db',
help='Force dry run to use a local DB "tradesv3.dry_run.sqlite" instead of memory DB. Work only if dry_run is \
enabled.', # noqa
action='store_true',
dest='dry_run_db',
)
build_subcommands(parser) build_subcommands(parser)
parsed_args = parser.parse_args(args) parsed_args = parser.parse_args(args)

View File

@ -39,7 +39,7 @@ def test_throttle_with_assets():
def test_parse_args_defaults(): def test_parse_args_defaults():
args = parse_args([]) args = parse_args([], '')
assert args is not None assert args is not None
assert args.config == 'config.json' assert args.config == 'config.json'
assert args.dynamic_whitelist is None assert args.dynamic_whitelist is None
@ -48,46 +48,46 @@ def test_parse_args_defaults():
def test_parse_args_invalid(): def test_parse_args_invalid():
with pytest.raises(SystemExit, match=r'2'): with pytest.raises(SystemExit, match=r'2'):
parse_args(['-c']) parse_args(['-c'], '')
def test_parse_args_config(): def test_parse_args_config():
args = parse_args(['-c', '/dev/null']) args = parse_args(['-c', '/dev/null'], '')
assert args is not None assert args is not None
assert args.config == '/dev/null' assert args.config == '/dev/null'
args = parse_args(['--config', '/dev/null']) args = parse_args(['--config', '/dev/null'], '')
assert args is not None assert args is not None
assert args.config == '/dev/null' assert args.config == '/dev/null'
def test_parse_args_verbose(): def test_parse_args_verbose():
args = parse_args(['-v']) args = parse_args(['-v'], '')
assert args is not None assert args is not None
assert args.loglevel == 10 assert args.loglevel == 10
def test_parse_args_dynamic_whitelist(): def test_parse_args_dynamic_whitelist():
args = parse_args(['--dynamic-whitelist']) args = parse_args(['--dynamic-whitelist'], '')
assert args is not None assert args is not None
assert args.dynamic_whitelist is 20 assert args.dynamic_whitelist is 20
def test_parse_args_dynamic_whitelist_10(): def test_parse_args_dynamic_whitelist_10():
args = parse_args(['--dynamic-whitelist', '10']) args = parse_args(['--dynamic-whitelist', '10'], '')
assert args is not None assert args is not None
assert args.dynamic_whitelist is 10 assert args.dynamic_whitelist is 10
def test_parse_args_dynamic_whitelist_invalid_values(): def test_parse_args_dynamic_whitelist_invalid_values():
with pytest.raises(SystemExit, match=r'2'): with pytest.raises(SystemExit, match=r'2'):
parse_args(['--dynamic-whitelist', 'abc']) parse_args(['--dynamic-whitelist', 'abc'], '')
def test_parse_args_backtesting(mocker): def test_parse_args_backtesting(mocker):
backtesting_mock = mocker.patch( backtesting_mock = mocker.patch(
'freqtrade.optimize.backtesting.start', MagicMock()) 'freqtrade.optimize.backtesting.start', MagicMock())
args = parse_args(['backtesting']) args = parse_args(['backtesting'], '')
assert args is None assert args is None
assert backtesting_mock.call_count == 1 assert backtesting_mock.call_count == 1
@ -102,10 +102,10 @@ def test_parse_args_backtesting(mocker):
def test_parse_args_backtesting_invalid(): def test_parse_args_backtesting_invalid():
with pytest.raises(SystemExit, match=r'2'): with pytest.raises(SystemExit, match=r'2'):
parse_args(['backtesting --ticker-interval']) parse_args(['backtesting --ticker-interval'], '')
with pytest.raises(SystemExit, match=r'2'): with pytest.raises(SystemExit, match=r'2'):
parse_args(['backtesting --ticker-interval', 'abc']) parse_args(['backtesting --ticker-interval', 'abc'], '')
def test_parse_args_backtesting_custom(mocker): def test_parse_args_backtesting_custom(mocker):
@ -116,7 +116,7 @@ def test_parse_args_backtesting_custom(mocker):
'backtesting', 'backtesting',
'--live', '--live',
'--ticker-interval', '1', '--ticker-interval', '1',
'--refresh-pairs-cached']) '--refresh-pairs-cached'], '')
assert args is None assert args is None
assert backtesting_mock.call_count == 1 assert backtesting_mock.call_count == 1
@ -133,7 +133,7 @@ def test_parse_args_backtesting_custom(mocker):
def test_parse_args_hyperopt(mocker): def test_parse_args_hyperopt(mocker):
hyperopt_mock = mocker.patch( hyperopt_mock = mocker.patch(
'freqtrade.optimize.hyperopt.start', MagicMock()) 'freqtrade.optimize.hyperopt.start', MagicMock())
args = parse_args(['hyperopt']) args = parse_args(['hyperopt'], '')
assert args is None assert args is None
assert hyperopt_mock.call_count == 1 assert hyperopt_mock.call_count == 1
@ -147,7 +147,7 @@ def test_parse_args_hyperopt(mocker):
def test_parse_args_hyperopt_custom(mocker): def test_parse_args_hyperopt_custom(mocker):
hyperopt_mock = mocker.patch( hyperopt_mock = mocker.patch(
'freqtrade.optimize.hyperopt.start', MagicMock()) 'freqtrade.optimize.hyperopt.start', MagicMock())
args = parse_args(['-c', 'test_conf.json', 'hyperopt', '--epochs', '20']) args = parse_args(['-c', 'test_conf.json', 'hyperopt', '--epochs', '20'], '')
assert args is None assert args is None
assert hyperopt_mock.call_count == 1 assert hyperopt_mock.call_count == 1