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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:38:21+00:00 2026-05-28T14:38:21+00:00

I am trying to get three arguments from command line: -o (for outputfile) -k

  • 0

I am trying to get three arguments from command line:

-o (for outputfile) -k (number of clusters) -l (data to be clustered)

So i wrote this.

def get_input():
print 'ARGV      :', sys.argv[1:]

options, remainder = getopt.getopt(sys.argv[1:], 'o:v:k:l', ['output=', 
                                                     'verbose',
                                                     'k_clust=',
                                                     'limit='])
print "options ",options
file_flag , k_flag, count_flag = False, False,False
for opt, arg in options:
    print opt
    if opt in ('-o', '--output'):
        print "here ", opt, arg
        output_filename = arg
        o_flag = True

    if opt in ('-v', '--verbose'):
        verbose = True
    if opt == '--version':
        version = arg

    if opt in ('-k','--k_clust'):
        print "here", opt, arg
        k_clust = arg
        k_flag = True

    if opt in ('-l','--limit'):
         kcount = arg
         assert kcount!=0 and kcount!= ''
         print "limit ", arg
         count_flag = True
if k_flag == False:
    sys.exit(" no cluster specified, will be exiting now")
if o_flag == False:
    print "using default outfile name ",output_filename
if count_flag == False:
   kcount = 10000000


return output_filename, k_clust,kcount

Everything is working on fine except the -l flag
so if my command line command is this:

$python foo.py -o foo.txt -k 2 -l 2

and the print argv prints

ARGV      : ['-o', 'demo.txt', '-k', '2', '-l', '2']

but the options is:

options  [('-o', 'demo.txt'), ('-k', '2'), ('-l', '')]

Notice that nothing is being parsed in the “l” field.
Wat am i doing wrong?
Thanks

  • 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-28T14:38:22+00:00Added an answer on May 28, 2026 at 2:38 pm

    getopt is a rather old module. If you have Python2.7, use argparse.
    If you have a slightly older version of Python >= 2.3, you can still install argparse:

    With

    import argparse
    parser=argparse.ArgumentParser()
    parser.add_argument('-o', help = 'outputfile')
    parser.add_argument('-k', help = 'number of clusters')
    parser.add_argument('-l', help = 'data to be clustered')
    args=parser.parse_args()
    print(args)
    

    running

    test.py -o foo.txt -k 2 -l 2
    

    yields

    Namespace(k='2', l='2', o='foo.txt')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to run a function in octave from the command line. The
Currently I am trying to pass some values through command line arguments and then
I'm trying to use optparse to parse command line arguments. I would like my
I have been trying to get yui-css grid system to make a three column
Hey there, I am trying to get my signout button to work. This is
I'm trying get values from a GridView using the following code: foreach (GridViewRow row
Trying to get my css / C# functions to look like this: body {
I am trying to get histogram from my color (3-channel) image in OpenCV but
I'm writing a Perl program that will take a few command-line arguments (they'll actually
I am trying to run this snippet from http://www.ibm.com/developerworks/linux/library/l-prog3.html on a python 2.6 runtime.

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.