GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Handle no arguments when using sub-commands in argparse · Issue #99735 · python/cpython

Handle no arguments when using sub-commands in argparse #99735

New issue
Copy link
New issue
Copy link
Closed
Closed
Handle no arguments when using sub-commands in argparse#99735
Copy link
Labels
docsDocumentation in the Doc dirDocumentation in the Doc dir

Description

@ppaez
ppaez
opened on Nov 23, 2022
Issue body actions

Documentation

The example that shows the use of sub-commands and set_defaults to dispatch a function for each sub-command, fails when there are no command line arguments:

AttributeError: 'Namespace' object has no attribute 'func'

A pull request is provided that adds one line to handle that case and an explanation in the documentation.

Linked PRs

  • gh-99735: Handle no arguments when using sub-commands in argparse #99736
  • gh-99735: Use required=True in argparse subparsers example #100927
  • [3.11] gh-99735: Use required=True in argparse subparsers example (GH-100927) #102036
  • [3.10] gh-99735: Use required=True in argparse subparsers example (GH-100927) #102037

Activity

  1. ppaez
    added
    docsDocumentation in the Doc dirDocumentation in the Doc dir
    on Nov 23, 2022
  2. ppaez
    changed the title [-]Handle no arguments when using sub-commands[/-] [+]Handle no arguments when using sub-commands in argparse[/+] on Nov 23, 2022
  3. hpaulj commented on Dec 8, 2022

    @hpaulj
    hpaulj
    on Dec 8, 2022
    More actions

    Originally subparsers were required, so this issue did not come up. Because of a patching oversight, they are now optional, and we had to add the required parameter. I think this example would be better handled with a

     subparsers = parser.add_subparsers(required=True)
    

    Then we wouldn't have to define a func for the no-subparser case. Your patch works, but I think it adds too much to the text and example, detracting from the essence of what set_defaults adds.

    Whether we use set_defaults or not, the Namespace will have different attributes depending on what subparser (or none) the user invokes. The post parsing code has to handle that issue in one way or the other.

  4. ppaez commented on Jan 10, 2023

    @ppaez
    ppaez
    on Jan 10, 2023
    ContributorAuthor
    More actions

    I hadn't used the required=True parameter before. It is mentioned in the reference, but I didn't consider it. I also was not aware of those changes (bpo-9253 and bpo-26510), thanks for explaining them.

    PR #100927 has been created with only that change.

  5. erlend-aasland
    added this to Argparse issueson Feb 1, 2023
  6. hauntsaninja
    added a commit that references this issue on Feb 19, 2023

    gh-99735: Use required=True in argparse subparsers example (#100927)

    6aab56f
  7. miss-islington
    added 4 commits that reference this issue on Feb 19, 2023

    pythongh-99735: Use required=True in argparse subparsers example (pyt…

    0288d9b

    pythongh-99735: Use required=True in argparse subparsers example (pyt…

    6f8c2be

    gh-99735: Use required=True in argparse subparsers example (GH-100927)

    4969903

    gh-99735: Use required=True in argparse subparsers example (GH-100927)

    f193fad
  8. carljm
    added a commit that references this issue on Feb 20, 2023

    Merge branch 'main' into inlinecomp2

    9f0fc5b
  9. erlend-aasland commented on Feb 21, 2023

    @erlend-aasland
    erlend-aasland
    on Feb 21, 2023
    Contributor
    More actions

    Can we close this issue?

  10. erlend-aasland
    added
    pendingThe issue will be closed if no feedback is providedThe issue will be closed if no feedback is provided
    on Feb 21, 2023
  11. erlend-aasland
    moved this to Doc issues in Argparse issueson Feb 21, 2023
  12. hauntsaninja commented on Feb 21, 2023

    @hauntsaninja
    hauntsaninja
    on Feb 21, 2023
    Contributor
    More actions

    Yes we can! The backports had a hiccup, but they're all done now

    Reacted by Erlend E. Aasland
  13. hauntsaninja
    closed this as completedon Feb 21, 2023
  14. hauntsaninja
    removed
    pendingThe issue will be closed if no feedback is providedThe issue will be closed if no feedback is provided
    on Feb 21, 2023
  15. erlend-aasland
    moved this from Doc issues to Done in Argparse issueson Feb 21, 2023
  16. carljm
    added a commit that references this issue on Feb 22, 2023

    Merge branch 'main' into inlinecomp

    544b30c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dirDocumentation in the Doc dir

    Projects

    • Argparse issues
      Status
      Done
      Show more project fields

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.