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

  • Home
  • SEARCH
  • 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 8613945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:00:04+00:00 2026-06-12T05:00:04+00:00

As described in the accepted answer to Most pythonic way of accepting arguments using

  • 0

As described in the accepted answer to Most pythonic way of accepting arguments using optparse, I have a program with a function that performs operations on a string. The program uses argparse to check whether the string is provided as-is or in a file and massages the input as needed to pass it to the function.

Now I want to extend the program with a more advanced version of my function, but still leave the basic version in for comparison, somewhat as in Use argparse to run 1 of 2 functions in my script. Where I believe my situation differs is that regardless of the function that gets called, I want the option of also passing my existing input flags.

Just adding a new argument to the parser and nesting my previous code inside a if/else that checks for that flag doesn’t work: it complains about the wrong number of arguments. I am aware of sub-commands, but I am still pretty new with argparse and it just seems like that would be overkill for what I want – but maybe not.

tl;dr: I need to choose one of two functions and one of two input types; both input types apply to both functions. Thanks for any help!

Edited to add code:

p = argparse.ArgumentParser(description="program.py")
p.add_argument("-e", dest='extended')   #The new flag causing the trouble
p.add_argument("-s", dest="string")
p.add_argument("-f", dest="infile")

args = p.parse_args()

if args.extended:
    if args.infile:
        with open(args.infile,'r') as f:
            for line in enumerate(f.readlines()):
                print 'Input: ', line[1],    
                output = funcExtended(line[1])  #new and improved function
                print 'Extended output: ', output
    elif args.string:
        output = funcExtended(args.string)
        print output
    else:  #my future default option to grab strings from a database
        print 'This will soon work: extended'
else:   #I fully realize that I shouldn't have to essentially copy and paste here
    if args.infile:
        with open(args.infile,'r') as f:
            for line in enumerate(f.readlines()):
                print 'Input: ', line[1],    
                output = funcBasic(line[1])  #old and tired function
                print 'Basic output: ', output
    elif args.string:
        output = funcBasic(args.string)
        print output
    else:   #my future default option to grab strings from a database
        print 'This will soon work: basic'

This is a command line utility. Issuing

$ python program.py -s 'string'

returns a properly formatted string, as before. But issuing

$ python program.py -s 'string' -e

returns

program.py: error: argument -e: expected one argument

Whew. Thanks again to anybody who can help!

  • 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-12T05:00:06+00:00Added an answer on June 12, 2026 at 5:00 am

    If you change your extended argument to a boolean flag with

    p.add_argument("-e", dest='extended', action="store_true")
    

    it will no longer expect an argument. You can then invoke your script with

    $ python program.py -e -s 'string'
    

    Finally as a bonus here are some ideas to make your code less redundant:

    import argparse
    
    def funcExtended(line):
       return " ".join(line)
    
    def funcBasic(line):
        return line.upper()
    
    p = argparse.ArgumentParser(description="program.py")
    p.add_argument("-e", "--extended", dest="func", action="store_const", const=funcExtended, default=funcBasic)
    p.add_argument("-s", "--string")
    p.add_argument("-f", "--infile")
    
    args = p.parse_args()
    
    def readlines(args):
        if args.infile:
            with open(args.infile,'r') as f:
                for line in f:
                    yield line.rstrip("\n")
        elif args.string:
            yield args.string
        else:  #my future default option to grab strings from a database
            print 'This will soon work: extended'
    
    for line in readlines(args):
        print 'Input: ', line
        output = args.func(line)
        print "Output: ", output
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables described below. What I need is a single query that
I implemented gesture recognizer in UIMapView just as described in the accepted answer to
Here is well described how to call member function by pointer: http://www.newty.de/fpt/functor.html But the
The CSS gradient is described here , but I have no idea how to
As the title described, how to use c library function fgets in assembly language?
What is the accepted, portable way to include interpreter options in the shebang line,
In both of the following StackOverflow questions, the accepted answer describes how to merge
First of all, I know the standard answer will be that exceptions are never
I have 3 tables - posts, friendships, and likes -- the important fields described
I'm writing a chat program in Python that needs to connect to a server

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.