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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:25:38+00:00 2026-05-23T18:25:38+00:00

I am trying to write a command line interface (for the first time) and

  • 0

I am trying to write a command line interface (for the first time) and after reading up about argparse, optparse and getopt I chose argparse because of several recommendations here on SO and elswhere in the net. Adapting a little of the advice of Mr. van Rossum I hooked up my first command line interface like this:

def main(argv=None):
    if argv is None:
        argv = sys.argv
    desc = u'some description'
    parser = argparse.ArgumentParser(description=desc)

    parser.add_argument('-s', '--search', help='Search for someone.')
    parser.add_argument('-c', '--do_something_else', help='Do something else.')

    args = parser.parse_args()
    print args

if __name__ == '__main__':
    sys.exit(main())

Doing python myscript.py -h results in:

usage: dblp.py [-h] [-s SEARCH] [-c DO_SOMETHING_ELSE]

some description

optional arguments:
  -h, --help            show this help message and exit
  -s SEARCH, --search SEARCH
                        Search for someone.
  -c DO_SOMETHING_ELSE, --do_something_else DO_SOMETHING_ELSE
                        Do something else.

So my first question is: Why are SEARCH and DO_SOMETHING_ELSE written in CAPITAL LETTERS? The second question would be: Do I break any standards? Is there a better way (ore a nice real world example I can learn from) how to build clean and useful command line interfaces with python? And are there pitfalls one should avoid, when writing cmd interfaces?

  • 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-05-23T18:25:38+00:00Added an answer on May 23, 2026 at 6:25 pm

    Here are a couple basic argparse resources I dug up:

    Some slides on argparse:
    http://www.slideshare.net/tisto/argparse-python-command-line-parser

    A little argparse example:
    http://www.rutherfurd.net/tag/argparse/

    Here is a real-world argparse example:
    https://github.com/harijay/xtaltools/blob/e683fcef6a5ad7394b87382e58d4dce32a85585b/maskconvert.py

    As far as avoiding pitfalls, here is a nice looking wrapper for argparse to reduce the boilerplate code you have to write for some common usecases:
    http://travelingfrontiers.wordpress.com/2010/11/03/simple-python-argparse-wrapper/

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

Sidebar

Related Questions

I am trying to write a GUI program for a command line program in
I'm trying to write a command line script to access Apple SDK docsets (like
I'm trying to write a simple command line audio player using the Python Gstreamer
I'm trying to write a GNU-style command-line parser for Go, since the flags package
I'm trying to write a python script which follows the common unix command line
I am trying to write a wrapper script for a command line program (svnadmin
I'm trying to write a piece of code in python to get command-line options
I'm trying to write a multi-threaded program, the number of threads based on command-line
I've two questions. I'm trying to write a script which takes command line arguments
I am trying to write my own Command Line wrapper like 'Console2' in C#.

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.