Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error when run with no command given #329

Merged
merged 3 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion tabcmd/execution/parent_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def parent_parser_with_global_options():
"-v",
"--version",
action="version",
version=strings[6] + "v" + version + "\n \n",
version=strings[6] + " v" + version + "\n \n",
help=strings[7],
)

Expand Down Expand Up @@ -182,6 +182,7 @@ def include(self, command):
command.define_args(additional_parser)
return additional_parser

# Help isn't added like the others because it has to have access to the rest, to get their args
def include_help(self):
additional_parser = self.subparsers.add_parser("help", help=strings[14], parents=[self.global_options])
additional_parser._optionals.title = strings[1]
Expand Down
17 changes: 9 additions & 8 deletions tabcmd/execution/tabcmd_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,33 @@ def run(parser, user_input=None):
sys.exit(0)
user_input = user_input or sys.argv[1:]
namespace = parser.parse_args(user_input)
# if no subcommand was given, call help
if not hasattr(namespace, "func"):
print("No command found.")
parser.print_help()
sys.exit(0)

if hasattr("namespace", "logging_level") and namespace.logging_level != logging.INFO:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this line need updating too? hasattr(namespace

print("logging:", namespace.logging_level)

logger = log(__name__, namespace.logging_level or logging.INFO)
logger.info("Tabcmd {}".format(version))
if (hasattr("namespace", "password") or hasattr("namespace", "token_value")) and hasattr("namespace", "func"):
if hasattr(namespace, "password") or hasattr(namespace, "token_value"):
# don't print whole namespace because it has secrets
logger.debug(namespace.func)
else:
logger.debug(namespace)
if namespace.language:
if hasattr(namespace, "language"):
set_client_locale(namespace.language, logger)
if namespace.query_page_size:
os.environ["TSC_PAGE_SIZE"] = str(namespace.query_page_size)

try:
func = namespace.func
# if a subcommand was identified, call the function assigned to it
# this is the functional equivalent of the call by reflection in the previous structure
# https://stackoverflow.com/questions/49038616/argparse-subparsers-with-functions
namespace.func.run_command(namespace)
except AttributeError:
parser.error("No command identified or too few arguments")
except Exception as e:
# todo: use log_stack here for better presentation?
logger.exception(e)
# if no command was given, argparse will just not create the attribute
sys.exit(2)

return namespace
Loading