Make config file a positional argument, as it is required

- Test invalid config file path throws. Remove redundant cli test

- Simplify cli parser code
  - Do not need to explicitly check if args.config_file set.
    argparser checks for positional arguments automatically

- Use standard semantics for cli args
  - All positional args are required. Non positional args are optional

- Improve command line --help description
This commit is contained in:
Debanjum Singh Solanky 2022-08-05 00:24:12 +03:00
parent a3b35fbb6e
commit ca5a8bd113
2 changed files with 13 additions and 23 deletions

View file

@ -10,12 +10,9 @@ from src.utils.helpers import is_none_or_empty, get_absolute_path, resolve_absol
from src.utils.rawconfig import FullConfig from src.utils.rawconfig import FullConfig
def cli(args=None): def cli(args=None):
if is_none_or_empty(args):
return None
# Setup Argument Parser for the Commandline Interface # Setup Argument Parser for the Commandline Interface
parser = argparse.ArgumentParser(description="Expose API for Khoj") parser = argparse.ArgumentParser(description="Start Khoj; A Natural Language Search Engine for your personal Notes, Transactions and Photos")
parser.add_argument('--config-file', '-c', type=pathlib.Path, help="YAML file with user configuration") parser.add_argument('config_file', type=pathlib.Path, help="YAML file to configure Khoj")
parser.add_argument('--regenerate', action='store_true', default=False, help="Regenerate model embeddings from source files. Default: false") parser.add_argument('--regenerate', action='store_true', default=False, help="Regenerate model embeddings from source files. Default: false")
parser.add_argument('--verbose', '-v', action='count', default=0, help="Show verbose conversion logs. Default: 0") parser.add_argument('--verbose', '-v', action='count', default=0, help="Show verbose conversion logs. Default: 0")
parser.add_argument('--host', type=str, default='127.0.0.1', help="Host address of the server. Default: 127.0.0.1") parser.add_argument('--host', type=str, default='127.0.0.1', help="Host address of the server. Default: 127.0.0.1")
@ -24,12 +21,8 @@ def cli(args=None):
args = parser.parse_args(args) args = parser.parse_args(args)
if not (args.config_file): if not resolve_absolute_path(args.config_file).exists():
print(f"Need --config-file flag to be passed from commandline") raise ValueError(f"Config file {args.config_file} does not exist")
exit(1)
elif not resolve_absolute_path(args.config_file).exists():
print(f"Config file {args.config_file} does not exist")
exit(1)
# Read Config from YML file # Read Config from YML file
config_from_file = None config_from_file = None

View file

@ -1,5 +1,9 @@
# Standard Modules # Standard Modules
from pathlib import Path from pathlib import Path
from random import random
# External Modules
import pytest
# Internal Packages # Internal Packages
from src.utils.cli import cli from src.utils.cli import cli
@ -9,7 +13,7 @@ from src.utils.cli import cli
# ---------------------------------------------------------------------------------------------------- # ----------------------------------------------------------------------------------------------------
def test_cli_minimal_default(): def test_cli_minimal_default():
# Act # Act
actual_args = cli(['--config-file=tests/data/config.yml']) actual_args = cli(['tests/data/config.yml'])
# Assert # Assert
assert actual_args.config_file == Path('tests/data/config.yml') assert actual_args.config_file == Path('tests/data/config.yml')
@ -17,22 +21,15 @@ def test_cli_minimal_default():
assert actual_args.verbose == 0 assert actual_args.verbose == 0
# ---------------------------------------------------------------------------------------------------- # ----------------------------------------------------------------------------------------------------
def test_cli_flags(): def test_cli_invalid_config_file_path():
# Act # Act
actual_args = cli(['--config-file=tests/data/config.yml', with pytest.raises(ValueError):
'--regenerate', cli([f"non-existent-khoj-{random()}.yml"])
'-vvv'])
# Assert
assert actual_args.config_file == Path('tests/data/config.yml')
assert actual_args.regenerate == True
assert actual_args.verbose == 3
# ---------------------------------------------------------------------------------------------------- # ----------------------------------------------------------------------------------------------------
def test_cli_config_from_file(): def test_cli_config_from_file():
# Act # Act
actual_args = cli(['--config-file=tests/data/config.yml', actual_args = cli(['tests/data/config.yml',
'--regenerate', '--regenerate',
'-vvv']) '-vvv'])