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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:48:24+00:00 2026-06-10T09:48:24+00:00

Is there an easy way to add arguments with values to python3 ‘s cmd.Cmd

  • 0

Is there an easy way to add arguments with values to python3‘s cmd.Cmd?

For example, it’s easy to implement commands with simple parameters:

> action1 param1 param2

by adding do_action1() and complete_action1(), to complete I can search the list of existing parameters.

But how would you implemented parameters with values, for example:

> action1 param1=234 param2=SomeTextValue

or

> action1 param1 234 param2 SomeTextValue

where param1 would be set to 234 and param2 to SomeTextValue.

Can this be parsed by cmd.Cmd?

I can think only about getting the whole argument list in do_action1( self, params = None ) and then parse it myself.

If I use optparse the parameters would have to be prefixed with dash like -p or --param1 and --param2, so to get completion in cmd.Cmd I would have to type 2 dashes first …

> action1 --param1=234 --param2=SomeTextValue

If I have to manually parse the parameters is there any python3 module like optparse which does not expect the parameters to have 2 dash prefix?

Any suggestions?

  • 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-10T09:48:26+00:00Added an answer on June 10, 2026 at 9:48 am

    Well, cmd.Cmd doesn’t actually do a whole lot of parsing for you, anyway. So, yes, you can handle parameters with arguments simply by completing parameters with a trailing =, and parsing the commands yourself:

    import cmd
    
    def parse(arg):
        return tuple(k.partition('=') for k in arg.split())
    
    class MyShell(cmd.Cmd):
        def do_foo(self, arg):
            for x, _, y in parse(arg):
                print(x, y)
    
        def complete_foo(self, text, line, begidx, endidx):
            # Cmd treats = as the end of params; we don't want that.
            if line.endswith('='):
                return ()
    
            opts = ['param1=', 'param2', 'param3=']
            return [opt for opt in opts if opt.startswith(text)]
    
    MyShell().cmdloop()
    

    Example usage:

    (Cmd) foo param<TAB>
    param1=  param2   param3=  
    (Cmd) foo param1=a param
    param1=  param2   param3=  
    (Cmd) foo param1=a param2 param3=ah
    param1 a
    param2 
    param3 ah
    

    Note that our completions contain a trailing = as a hint to the user, to let them know a parameter can be passed.

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

Sidebar

Related Questions

I was wondering if there was an easy way to add parameters to a
Is there an easy way to add a custom RFC822 header to a message
Is there an easy way to add line number information to created nodes in
All I want to know is if there's an easy way to add lines
Is there an easy way to add an Text field to an UIAlertView an
Is there an easy way to add the members of two equally sized list
Is there an easy way to add expires headers to static content coming from
Is there a easy way to add a search capability on fields in Django?
Is there an easy way to add a record/row to a numpy recarray without
Is there an easy way to add syntax highlighting to my various plugin's gh-pages

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.