Skip to content
Open
Changes from 1 commit
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
26 changes: 23 additions & 3 deletions nettacker/core/arg_parser.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
import sys
from argparse import ArgumentParser

import difflib
import yaml

from nettacker import all_module_severity_and_desc
Expand Down Expand Up @@ -383,7 +383,7 @@ def add_arguments(self):
method_options.add_argument(
"--set-hardware-usage",
action="store",
dest="set_hardware_usage",
deimport difflibst="set_hardware_usage",
Copy link

Copilot AI Mar 23, 2026

Choose a reason for hiding this comment

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

add_argument for --set-hardware-usage has a typo in the keyword argument name (deimport difflibst) which will raise a TypeError at runtime and prevents the option from being parsed. This should be dest="set_hardware_usage".

Suggested change
deimport difflibst="set_hardware_usage",
dest="set_hardware_usage",

Copilot uses AI. Check for mistakes.
default=Config.settings.set_hardware_usage,
help=_("set_hardware_usage"),
)
Expand Down Expand Up @@ -518,7 +518,27 @@ def parse_arguments(self):
all ARGS with applied rules
"""
# Checking Requirements
options = self.api_arguments or self.parse_args()
if self.api_arguments:
options = self.api_arguments
else:
known_args, unknown_args = self.parse_known_args()

if unknown_args:
valid_flags = []
for action in self._actions:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

self._actions is not defined anywhere. This code will break.

valid_flags.extend(action.option_strings)

for arg in unknown_args:
if arg.startswith("-") and len(arg) > 1:
suggestion = difflib.get_close_matches(arg, valid_flags, n=1)
if suggestion:
print(f"Error: Unknown argument '{arg}'. Did you mean '{suggestion[0]}'?")
Copy link

Copilot AI Mar 23, 2026

Choose a reason for hiding this comment

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

Indentation on the print(...) line is inconsistent with the surrounding block (it’s indented less than the if suggestion: body). This will raise an IndentationError and break CLI parsing.

Suggested change
print(f"Error: Unknown argument '{arg}'. Did you mean '{suggestion[0]}'?")
print(f"Error: Unknown argument '{arg}'. Did you mean '{suggestion[0]}'?")

Copilot uses AI. Check for mistakes.
else:
print(f"Error: Unknown argument '{arg}'")

Comment on lines +532 to +544
Copy link

Copilot AI Mar 23, 2026

Choose a reason for hiding this comment

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

If an unknown argument is positional (doesn’t start with -), the code still exits with status 1 but prints no error message. Ensure all entries in unknown_args are surfaced to the user (positional and flags), ideally in a single consolidated message.

Suggested change
for arg in unknown_args:
if arg.startswith("-") and len(arg) > 1:
suggestion = difflib.get_close_matches(arg, valid_flags, n=1)
if suggestion:
print(f"Error: Unknown argument '{arg}'. Did you mean '{suggestion[0]}'?")
else:
print(f"Error: Unknown argument '{arg}'")
error_messages = []
for arg in unknown_args:
# Flag-like unknown argument (starts with "-" and not just "-")
if arg.startswith("-") and len(arg) > 1:
suggestion = difflib.get_close_matches(arg, valid_flags, n=1)
if suggestion:
error_messages.append(
f" {arg} (did you mean '{suggestion[0]}'?)"
)
else:
error_messages.append(f" {arg}")
# Positional unknown argument
else:
error_messages.append(f" {arg} (positional argument)")
if error_messages:
print(
"Error: Unknown arguments:\n" + "\n".join(error_messages)
)

Copilot uses AI. Check for mistakes.
sys.exit(1)

Comment on lines +532 to +546
Copy link

Copilot AI Mar 23, 2026

Choose a reason for hiding this comment

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

This path uses print(...) + sys.exit(1) for error handling, which bypasses the project’s existing die_failure() behavior (logging + color reset) and prints to stdout instead of stderr. Consider using die_failure(...) (or ArgumentParser.error(...) if you want argparse-style usage output) and include the suggestion text in that message.

Suggested change
for arg in unknown_args:
if arg.startswith("-") and len(arg) > 1:
suggestion = difflib.get_close_matches(arg, valid_flags, n=1)
if suggestion:
print(f"Error: Unknown argument '{arg}'. Did you mean '{suggestion[0]}'?")
else:
print(f"Error: Unknown argument '{arg}'")
sys.exit(1)
error_messages = []
for arg in unknown_args:
if arg.startswith("-") and len(arg) > 1:
suggestion = difflib.get_close_matches(arg, valid_flags, n=1)
if suggestion:
error_messages.append(
f"Error: Unknown argument '{arg}'. Did you mean '{suggestion[0]}'?"
)
else:
error_messages.append(f"Error: Unknown argument '{arg}'")
if error_messages:
die_failure("\n".join(error_messages))

Copilot uses AI. Check for mistakes.
options = known_args
Comment on lines +525 to +547
Copy link

Copilot AI Mar 23, 2026

Choose a reason for hiding this comment

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

New behavior around unknown-argument detection/suggestion is introduced here, but there are no existing tests covering ArgParser behavior. Please add pytest coverage for cases like an unknown flag with a close match (suggestion emitted) and an unknown positional argument (error message emitted).

Copilot uses AI. Check for mistakes.

if options.language not in self.languages:
die_failure("Please select one of these languages {0}".format(self.languages))
Expand Down
Loading