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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T19:32:57+00:00 2026-05-20T19:32:57+00:00

The following parser.add_option statements work but if the script is run without an option/arg

  • 0

The following “parser.add_option” statements work but if the script is run without an option/arg it will not complain. If an option/argument are not specified I would like it to display help (-h / –help) as default.

usage = "usage: %prog [options] arg"
parser = OptionParser(usage)
parser.add_option('-d', '--directory',
        action='store', dest='directory',
        default=None, help='specify directory')
parser.add_option('-f', '--file',
        action='store', dest='filename',
        default=None, help='specify file')
parser.add_option('-v', '--version',
                  action="store_true", dest="show_version",
                  default=False, help='displays the version number')
(options, args) = parser.parse_args()
#if len(args) < 1:
#    parser.error("incorrect number of arguments")

Secondly, if I enable the following snip than I get “error: incorrect number of arguments” even when specifying an option/arg.

if len(args) < 1:
parser.error("incorrect number of arguments")

Thanks.


Updated Code with Traceback error below

def main():
    usage = "usage: %prog [options] arg"
    parser = OptionParser(usage)
    parser.add_option('-d', '--directory',
            action='store', dest='directory',
            default=None, help='specify directory')
    parser.add_option('-f', '--file',
            action='store', dest='filename',
            default=None, help='specify file')
    parser.add_option('-v', '--version',
                      action="store_true", dest="show_version",
                      default=False, help='displays the version number')
    if len(sys.argv) == 1:
        parser.print_help()
        sys.exit()
    (options, args) = parser.parse_args()

#if options.show_version:
#    prog = os.path.basename(sys.argv[0])
#    version_str = "1.0"
#    print "version is: %s %s" % (prog, version_str)
#    sys.exit(0)

filenames_or_wildcards = []

# remove next line if you do not want allow to run the script without the -f -d
# option, but with arguments
filenames_or_wildcards = args # take all filenames passed in the command line

# if -f was specified add them (in current working directory)
if options.filename is not None:
    filenames_or_wildcards.append(options.filename)

Traceback

$ python boto-backup.py Traceback (most recent call last):   File "boto-backup.py", line 41, in <module>
    filenames_or_wildcards = args # take all filenames passed in the command line NameError: name 'args' is not defined
  • 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-20T19:32:57+00:00Added an answer on May 20, 2026 at 7:32 pm

    I would do something like this:

    from optparse import OptionParser
    import sys
    
    def main():
        usage = "usage: %prog [options] arg"
        parser = OptionParser(usage)
        parser.add_option('-d', '--directory',
                action='store', dest='directory',
                default=None, help='specify directory')
        parser.add_option('-f', '--file',
                action='store', dest='filename',
                default=None, help='specify file')
        parser.add_option('-v', '--version',
                          action="store_true", dest="show_version",
                          default=False, help='displays the version number')
        if len(sys.argv) == 1:
            parser.print_help()
            sys.exit()
        (options, args) = parser.parse_args()
        # rest of program...
    
    if __name__ == '__main__':
        main()
    

    So we set up the parser and options, and then check if there was any command-line input.
    If none, we print the help message and exit. Otherwise we proceed with program execution.

    When run with no command-line arguments the output is:

    Usage: your_script_name_here.py [options] arg
    
    Options:
      -h, --help            show this help message and exit
      -d DIRECTORY, --directory=DIRECTORY
                            specify directory
      -f FILENAME, --file=FILENAME
                            specify file
      -v, --version         displays the version number
    

    Edit (in response to updated code):
    Whitespace/indentation is important in Python.
    Make sure that the rest of your code is indented such that it belongs to the main() function.
    From filenames_or_wildcards = [] on, your code is outside of the scope of the main() function and thus doesn’t have a variable named args.

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

Sidebar

Related Questions

No related questions found

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.