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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:37:25+00:00 2026-05-20T20:37:25+00:00

How can options/arguments determine which function is chosen during program execution? For the example

  • 0

How can options/arguments determine which function is chosen during program execution? For the example I have two options depending on the option I would like them to use their respective functions. What am I missing?

import os, sys, glob
from optparse import OptionParser

def fname(arguments):
    files = []
    for arg in arguments:
        if '*' in arg or '?' in arg:
            # contains a wildcard character
            files.extend(glob.glob(arg))
        elif os.path.isdir(arg):
            # is a dictionary
            files.extend(glob.glob(os.path.join(arg, '*')))
        elif os.path.exists(arg):
            # is a file
            files.append(arg)
        else:
            # invalid?
            print '%s invalid' % arg
    return files

# check if file exists locally, if not: download it
def downnload(filename, keyString):
    if not os.path.exists(filename+keyString):
        l.get_contents_to_filename(filename+keyString)

# List bucket contents
def blist(bucket):
    for b in rs:
        print b.name

def main():
    usage = "usage: %prog [options] -f filename"
    parser = OptionParser(usage)
    parser.add_option('-d', '--download',
            action='store', dest='download',
            default=None, help='download files from cloud')
    parser.add_option('-l', '--list',
            action='store', dest='bucket',
            default=None, help='list buckets or contents of specified bucket')

    if len(sys.argv) == 1:
        parser.print_help()
        sys.exit()
    (options, args) = parser.parse_args()

    # from boto import
    bucket_list = bucket.list()
    for l in bucket_list:
        keyString = str(l.key)

    downnload(options.filename, keyString)
    blist(options.bucket)

if __name__ == '__main__':
    main()
  • 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-20T20:37:26+00:00Added an answer on May 20, 2026 at 8:37 pm


    You are missing a lot.

    1. Where is filename given a value?
    2. Where is keyString given a value?
    3. Where is bucket given a value?
    4. You shouldn’t name your function list since that it is a primitive type

    You probably want to look at the optparse tutorial. I’m assuming that you expecting bucket to receive the value from the --list command line argument. The value gets stored into options.bucket instead. That is just a start.

    I think that you want to change the end of main to check the options and call the appropriate function. Something like:

    def main():
        # much omitted
        if options.filename is not None:
            downnload(options.filename, keyString)
        elif options.bucket is not None:
            blist(options.bucket)
        else:
            print 'Either --download or --list is required.'
            parser.print_help()
            sys.exit(-1)
    

    I think that this is what you are looking for. It decides which function to call based on the command line arguments passed in. For example, if the user passes --download filename then the downnload function is called with the supplied filename as the argument.

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

Sidebar

Related Questions

I have a pop up with plenty of options which can be edited -
I'm using Boost Program Options Library to parse the command line arguments. I have
In a plugin I'm writing, the dev can specify options, which I'm storing and
A there any g++ options which can detect improper initialization of std::string with NULL
I am learning Java and have been given these options: How can you implement
Consider and client server scenario and you got two options: You can include Server's
I'm writing a program which takes filenames and options on the command line in
GCC can get pretty picky about the order in which it accepts its arguments:
I have a script which takes command line arguments and after some processing, it
How can I, in a c program, perform a glob by using the function

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.