Skip to content

Commit

Permalink
Merge pull request #1715 from buildtesters/list_all_buildtest_opts_vi…
Browse files Browse the repository at this point in the history
…a_cli

List all buildtest options via command line
  • Loading branch information
shahzebsiddiqui authored Feb 26, 2024
2 parents 5e69e8f + 764fcfe commit d56dcdd
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 21 deletions.
1 change: 0 additions & 1 deletion .github/workflows/style.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ jobs:
with:
options: "--check --verbose"
src: "buildtest tests scripts"
version: "~= 23.3.0"

- name: Run yamllint
run: |
Expand Down
6 changes: 3 additions & 3 deletions bash_completion.sh
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ _buildtest_show_commands()

_buildtest_options()
{
python -c "from buildtest.cli import BuildTestParser; print(' '.join(BuildTestParser().get_buildtest_options()))"
buildtest --listopts
}
# entry point to buildtest bash completion function
_buildtest ()
Expand All @@ -162,13 +162,13 @@ _buildtest ()

COMPREPLY=() # Array variable storing the possible completions.

declare -a buildtest_opts=("--color" "--config" "--debug" "--editor" "--help" "--helpcolor" "--help-all" "--logpath" "--loglevel" "--print-log" "--no-color" "--report" "--verbose" "--version" "--view-log" "-c" "-d" "-h" "-l" "-p" "-r" "-H" "-V")
main_opts=($(_buildtest_options))

commands_with_input=( "--color" "--config" "-c" "--report" "-r" "--loglevel" "-l" "--editor" ) # Array variable storing commands which require an input argument from the user.

for command in "${COMP_WORDS[@]}"
do
for element in "${buildtest_opts[@]}"
for element in "${main_opts[@]}"
do

if [[ "$command" == "$element" ]]; then
Expand Down
33 changes: 16 additions & 17 deletions buildtest/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,6 @@ def __init__(self):

self._build_options()

# list used to store all main options for buildtest
self.main_options = self.get_buildtest_options()

self.hidden_subcommands = {
"docs": {},
"tutorial-examples": {},
Expand Down Expand Up @@ -347,6 +344,18 @@ def parse(self):
def get_subparsers(self):
return self.subparsers

def retrieve_main_options(self):
"""This method retrieves all options for buildtest command line interface. This is invoked by ``buildtest --listopts`` command and useful when user
wants to see all options."""
options_list = []

# Iterate over the actions of the parser to extract short and long options
for action in self.parser._actions:
option_strings = action.option_strings
options_list.extend(option_strings)

return sorted(options_list)

def _build_subparsers(self):
"""This method builds subparsers for buildtest command line interface."""

Expand Down Expand Up @@ -426,26 +435,16 @@ def _build_options(self):
["-H", "--help-all"],
{"help": "List all commands and options", "action": "help"},
),
(
["--listopts"],
{"action": "store_true", "help": "List all options for buildtest"},
),
(["--verbose"], {"action": "store_true", "help": "Enable verbose output"}),
]

for args, kwargs in self.buildtest_options:
self.parser.add_argument(*args, **kwargs)

def get_buildtest_options(self):
"""This method is used to return all main options for buildtest command line interface. This is useful for bash completion script
where we need to return all options for buildtest command line interface for tab completion.
"""
main_options = set()
for args, kwargs in self.buildtest_options:
for name in args:
main_options.add(name)

# adding -h and --help options
main_options.add("-h")
main_options.add("--help")
return list(sorted(main_options))

def get_subcommands(self):
"""Return a list of buildtest commands. This is useful for ``buildtest commands`` command to show a list of buildtest commands"""
return list(self.subcommands.keys()) + list(self.hidden_subcommands.keys())
Expand Down
3 changes: 3 additions & 0 deletions buildtest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ def main():

return

if args.listopts:
print("\n".join(parser.retrieve_main_options()))
return
# print the available color options in a table format if buildtest --helpcolor is specified
if args.helpcolor:
print_available_colors()
Expand Down
6 changes: 6 additions & 0 deletions tests/cli/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest

from buildtest.cli import (
BuildTestParser,
build_filters_format,
handle_kv_string,
positive_number,
Expand Down Expand Up @@ -81,3 +82,8 @@ def test_valid_time():
# raises exception when its unable to convert time
with pytest.raises(ValueError):
valid_time("2022-01-01 abcdef")


def test_retrieve_main_opts():
parser = BuildTestParser()
print(parser.retrieve_main_options())

0 comments on commit d56dcdd

Please sign in to comment.