Fix #113: Allow positional arguments after optional arguments - #151
Open
ahpooch wants to merge 1 commit into
Open
Fix #113: Allow positional arguments after optional arguments#151ahpooch wants to merge 1 commit into
ahpooch wants to merge 1 commit into
Conversation
…l arguments The kms_parser_check_optionals() function incorrectly rejected positional arguments that appeared after optional arguments, treating them as unknown options. Changed the validation logic in pykms_Misc.py (line 430) to only raise an error for unrecognized optional arguments (those starting with '-'), while allowing positional arguments to pass through. This fixes commands like: python3 pykms_Client.py -m Windows10 192.168.10.1 python3 pykms_Client.py -y 192.168.10.1 python3 pykms_Client.py -m Windows10 192.168.10.1 1688 Previously these would fail with 'expected X argument, unrecognized' errors. Now they work correctly regardless of argument order. Fixes Py-KMS-Organization#113
There was a problem hiding this comment.
Pull request overview
This PR addresses issue #113 by adjusting optional-argument validation so positional arguments placed after optionals (e.g., -m Windows10 192.168.10.1) are not incorrectly rejected as “unrecognized options”.
Changes:
- Updated
kms_parser_check_optionals()length-validation to only raise on unrecognized tokens that look like option flags (start with-), allowing subsequent positional args.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
427
to
431
| except IndexError: | ||
| pass | ||
|
|
||
| if elem and elem not in allarg: | ||
| if elem and elem.startswith('-') and elem not in allarg: | ||
| raise KmsParserException("%s argument `" %msg + found + "`:" + " expected " + num + " unrecognized: '%s'" %elem) |
Member
There was a problem hiding this comment.
How about the @ahpooch moves the if elem into a new else: section of the try-except and we remove the elem declaration to None from the top?
simonmicro
requested changes
Aug 7, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR fixes issue #113 where
pykms_Client.pyfails to parse command line arguments correctly when positional arguments are placed after optional arguments.Problem
The
kms_parser_check_optionals()function inpykms_Misc.pywas incorrectly validating command-line arguments. When a positional argument (like an IP address) appeared after an optional argument (like-m Windows10), the parser would treat it as an unknown option and raise an error.Example of the bug:
This happened because the validation logic checked if the element following an option was a known optional argument, and if not, raised an error - without considering that it might be a legitimate positional argument.
Solution
Modified the validation logic in
pykms_Misc.py(line 430) to only raise an error for unrecognized optional arguments (those starting with-), while allowing positional arguments to pass through.Change made:
This simple change ensures that:
-) are still caught and reported as errors-) are allowed after optional argumentsTesting
After the fix, all the following commands work correctly:
✅ With positional arguments after optional arguments:
✅ Traditional order (positional before optional) still works:
✅ Error handling for invalid options still works:
$ python3 pykms_Client.py -m InvalidMode 192.168.10.1 argument -m/--mode: invalid choice: 'InvalidMode' (choose from ...). Exiting...Impact
py-kms/pykms_Misc.py)Related Issues
Fixes #113
Checklist