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 8383057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:04:15+00:00 2026-06-09T17:04:15+00:00

I want this functionality: $ python program.py add Peter ‘Peter’ was added to the

  • 0

I want this functionality:

$ python program.py add Peter 
'Peter' was added to the list of names.

I can achieve this with --add instead of add like this:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--add", help="Add a new name to the list of names",
                    action="store")
args = parser.parse_args()
if args.add:
   print "'%s' was added to the list of names." % args.add
else:
   print "Just executing the program baby."

Such that:

$ python program.py --add Peter
'Peter' was added to the list of names.

But when I change --add to add it is no longer optional, how can I still let it be optional yet not have those -- signs? (preferably also using the argparse library)

  • 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-09T17:04:16+00:00Added an answer on June 9, 2026 at 5:04 pm

    What you want, is actually called “positional arguments”.
    You can parse them like this:

    import argparse                                                             
    parser = argparse.ArgumentParser()                                             
    parser.add_argument("cmd", help="Execute a command",                           
                        action="store", nargs='*')                                 
    args = parser.parse_args()                                                     
    if args.cmd:                                                                   
        cmd, name = args.cmd                                                       
        print "'%s' was '%s'-ed to the list of names." % (name, cmd)               
    else:                                                                          
        print "Just executing the program baby."                                   
    

    Which gives you the ability to specify different actions:

    $ python g.py add peter
    'peter' was 'add'-ed to the list of names.
    
    $ python g.py del peter
    'peter' was 'del'-ed to the list of names.
    
    $ python g.py 
    Just executing the program baby.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can't seem to achieve this simple functionality! All I want is to clear a
We alert user using this javascript, now i want to add this functionality to
I want to achieve the functionality that this editor has got with its preview
I want to add a dashboard functionality to an existing java web application, this
I want to expand on the functionality related to this question . Is there
i want the following functionality. input : this is test <b> bold text </b>
I want to draw a custom TabControl with custom functionality. To do this, i
How can I get cpuinfo in python 2.4. I want to determine number of
Suppose I want to use the Decorator pattern to add functionality to the java.lang.String
I am new to Python and want to use its plot functionality to create

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.