Skip to content

Commit

Permalink
Improve error message when subcommand is not provided
Browse files Browse the repository at this point in the history
Test case added
  • Loading branch information
LegenJCdary committed Mar 30, 2022
1 parent 2df5f69 commit c903825
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
9 changes: 7 additions & 2 deletions ansible_deployer/command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def parse_options(argv):
specific subcommand outside"""
parser = argparse.ArgumentParser(add_help=True)

parser.add_argument("subcommand", nargs=1, default=[None], metavar="SUBCOMMAND",
parser.add_argument("subcommand", nargs='?', default=None, metavar="SUBCOMMAND",
help='Specify command to run. Available commands: run, list, lock, unlock.')
parser.add_argument("--infrastructure", "-i", nargs=1, default=[None], metavar="INFRASTRUCTURE",
help='Specify infrastructure for deploy.')
Expand All @@ -81,8 +81,13 @@ def parse_options(argv):

arguments = parser.parse_args(argv)

if not arguments.subcommand:
print("[CRITICAL]: First positional argument (subcommand) is required! Available commands "
"are: run, list, lock, unlock.")
sys.exit(57)

options = {}
options["subcommand"] = arguments.subcommand[0]
options["subcommand"] = arguments.subcommand.lower()
verify_subcommand(options["subcommand"])

options["infra"] = arguments.infrastructure[0]
Expand Down
1 change: 1 addition & 0 deletions tests/01-test_argument_parsing.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ check_message_in_output 'ansible-deployer run' '\[ERROR\]: stage is required for
check_message_in_output 'ansible-deployer run -t testTask' '\[ERROR\]: stage is required for run'
check_message_in_output 'ansible-deployer run -t testTask' '\[ERROR\]: infra is required for run'
check_message_in_output 'ansible-deployer run -t testTask -i testInfra' 'stage is required for run'
check_message_in_output 'ansible-deployer -t testTask -i testInfra' '\[CRITICAL\]: First positional argument (subcommand) is required! Available commands are: run, list, lock, unlock.'

check_message_in_output 'ansible-deployer lock -t testTask -i testInfra' '\[ERROR\]: task is not supported by lock'
check_message_in_output 'ansible-deployer lock -t testTask -i testInfra -s prod' '\[ERROR\]: task is not supported by lock'
Expand Down

0 comments on commit c903825

Please sign in to comment.