Skip to content

Conversation

@bw2
Copy link
Owner

@bw2 bw2 commented Dec 31, 2025

Summary

This PR addresses two open issues by fixing the -- separator handling bug and documenting all available aliases.

Changes

1. Fix -- separator handling (Issue #298)

Problem: When using config files or environment variables with the -- separator, config/env args were incorrectly mixed with positional arguments that should come after --.

Example of the bug:

# With config file containing: list = [1, 2, 3]
./script.py -c config.toml -- foo bar

# Before fix (WRONG):
# positional_arg = ['foo', 'bar', '--list=3']  # Config arg leaked into positional!

# After fix (CORRECT):
# positional_arg = ['foo', 'bar']
# list = ['1', '2', '3']

Solution:

  • Modified parse_known_args() in configargparse.py (lines 971-982 and 1067-1078)
  • Config file and env var args are now inserted before the first optional argument (starting with -)
  • This preserves the -- separator position and prevents args from being mixed
  • Added comprehensive test case testDoubleDashSeparator() to verify the fix

Technical details:
The original code used the nargs flag to decide whether to append or prepend args. When action="append" was used, this caused config args to be appended to the end, placing them after --. The new implementation always inserts before the first optional arg, correctly handling the -- separator in all cases.

2. Document all available aliases (Issue #297)

Problem: ConfigArgParse provides many convenient aliases (like p.add(), p.parse(), ArgParser, etc.) that were used in examples but not properly documented.

Solution:

  • Enhanced the "Aliases" section in README.rst
  • Organized all aliases into clear categories:
    • Class Aliases: ArgParser, Parser
    • Method Aliases: p.add(), p.add_arg(), p.parse(), p.parse_known()
    • Function Aliases: get_parser(), get_arg_parser(), etc.
    • HelpFormatter Aliases: RawFormatter, DefaultsFormatter, etc.
  • Each alias now clearly shows what it maps to

Testing

  • Added new test case testDoubleDashSeparator() that tests:
    • Config file with -- separator
    • Config file without -- separator
    • Environment variables with -- separator
  • All existing tests pass (64 tests, only expected TOML failures due to missing optional dependency)

Closes

🤖 Generated with Claude Code

bw2 and others added 3 commits December 30, 2025 21:01
This commit addresses two issues:

1. Fix -- separator handling with config files and env vars (Issue #298)
   - Modified parse_known_args() to insert config file and env var args
     before the first optional argument (starts with -) instead of
     prepending/appending them
   - This preserves the -- separator position and ensures positional
     args after -- are not mixed with config/env var args
   - Added comprehensive test case testDoubleDashSeparator() to verify
     the fix works with config files and environment variables

2. Document all available aliases (Issue #297)
   - Enhanced the Aliases section in README.rst to document all
     available alias names
   - Organized aliases into clear categories: Class, Method, Function,
     and HelpFormatter aliases
   - Each alias now shows what it maps to for clarity

Technical details:
- The original code used the 'nargs' flag to decide whether to append
  or prepend args, which caused config args to be placed after --
  when action="append" was used
- The new implementation always inserts before the first optional arg,
  which correctly handles -- separator in all cases

🤖 Generated with Claude Code (https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ConfigArgParse is not handling the -- seperator for positional arguments correctly Aliases used in example code are not documented

2 participants