Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9038485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:30:01+00:00 2026-06-16T09:30:01+00:00

My goal is to write a Python command line utility using argparse that has

  • 0

My goal is to write a Python command line utility using argparse that has multiple commands that each have different sets of required inputs.

I tried reading through the docs, a few Google searches, and stack overflow and couldn’t find anything. I can think of a few possible solutions but they are ugly and require dealing with the help docs and validation manually. My suspicion is that this is the kind of problem with a common, effective solution already well enough known, and I am just lacking the right terms to search for it, or it is obscure enough by a small margin that it isn’t posted in many places.

The best idea I have right now is to have one positional argument, and somehow have different requirements for a set of additional arguments based on the value of that input. Maybe I will parse twice?

As an example, this is a similar case:

There is one positional argument, animal

Options for animal are cat, lizard, fish

For cat, arguments claws, whiskers, paws are required

For lizard, arguments scale_color, favorite_food are required

For fish, argument water_type is required

We want the required additional arguments for each different animal value to be documented in -h without resorting to unorthodox practice.

I considered doing this with an optional argument for each of the main category choices. This is unattractive because the utility really only wants to take one of those arguments, and if I can avoid reinventing the wheel in terms of enforcing and documenting this, I would prefer to.

I could do something like:

valid_commands = ['a','b','c','d','e','f','g']
parser.add_argument('command', choices = valid_commands)
parser.add_argument('inputs', nargs = '*')

But then I don’t have good input checks on the additional arguments for each command choice.

Is there some normal way this is done? Surely it is fairly common to write a utility that has several possible commands, and different required arguments for each. I could definitely get most of want I need by adding manual checks and help documentation, but this is the kind of thing I will probably do enough times that it’s worth it to get it right on the first try.

Thanks for reading and let me know if I can help provide information.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T09:30:02+00:00Added an answer on June 16, 2026 at 9:30 am

    This seems to be what subparsers are for?

    http://docs.python.org/dev/library/argparse.html#argparse.ArgumentParser.add_subparsers

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a mapper that, while processing data, classifies output into 3 different types
I want to be able to create a command line utility written in Scala
My goal is to write some sort of chatterbot that speaks in spanish. I've
I am using the python library docx: http://github.com/mikemaccana/python-docx My goal is to open a
Using python I have created following data frame which contains similarity values: cosinFcolor cosinEdge
I am trying to write a program in python or c that can debug
The Goal: Access / Write to the same temp files when using a common
Goal: write a query that can bring back all the users of a company,
I'm looking to write a Python import filter or preprocessor for source files that
I am trying to write a cross-platform python program that would run in the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.