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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:13:24+00:00 2026-06-18T08:13:24+00:00

I need to write a command line application, like a shell. So it will

  • 0

I need to write a command line application, like a shell. So it will include commands etc. The thing is I don’t know how to pass parameters to the funcions in a module. For example:

User writes: function1 folder1
Program should now pass the ‘folder1’ parameter to the function1 function, and run it. But also it has to support other functions with different parameters ex:

User input: function2 folder2 –exampleparam

How to make this to work? I mean, I could just write a module, import it in python and just use the python console, but this is not the case. I need a script that takes command input and runs it.

I tried to use eval(), but that doesn’t solve the problem with params. Or maybe it does but I don’t see it?

  • 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-18T08:13:25+00:00Added an answer on June 18, 2026 at 8:13 am

    The first part of your problem — parsing the command line — can be solved with argparse.

    The second part — converting the string name of a function into a function call — can be done with exec or a dispatching dict which maps from strings to function objects.

    I would recommend NOT using exec for this, since
    allowing a user to call arbitrary Python functions from the command line might be dangerous. Instead, make a whitelist of allowable functions:

    import argparse
    
    
    def foo(path):
        print('Running foo(%r)' % (path, ))
    
    
    def bar(path):
        print('Running bar(%r)' % (path, ))
    
    dispatch = {
        'foo': foo,
        'bar': bar,
    }
    
    parser = argparse.ArgumentParser()
    parser.add_argument('function')
    parser.add_argument('arguments', nargs='*')
    args = parser.parse_args()
    
    dispatch[args.function](*args.arguments)
    

    % test.py foo 1
    Running foo('1')
    % test.py bar 2
    Running bar('2')
    % test.py baz 3
    KeyError: 'baz'
    

    The above works when the command is typed into the command-line itself. If the command is being typed into stdin, then we’ll need to do something a bit different.

    A simple way would be to call raw_input to grab the string from stdin. We could then parse the string with argparse, as we did above:

    shmod.py:

    import argparse
    
    
    def foo(path):
        print('Running foo(%r)' % (path, ))
    
    
    def bar(path):
        print('Running bar(%r)' % (path, ))
    
    dispatch = {
        'foo': foo,
        'bar': bar,
    }
    
    def parse_args(cmd):
        parser = argparse.ArgumentParser()
        parser.add_argument('function')
        parser.add_argument('arguments', nargs='*')
        args = parser.parse_args(cmd.split())
        return args
    

    main.py:

    import shmod
    
    while True:
        cmd = raw_input('> ')
        args = shmod.parse_args(cmd)
        try:
            shmod.dispatch[args.function](*args.arguments)
        except KeyError:
            print('Invalid input: {!r}'.format(cmd))
    

    Another, more sophisticated way to handle this is to use the cmd module, as @chepner mentioned in the comments.

    from cmd import Cmd
    
    
    class MyInterpreter(Cmd):
    
        prompt = '> '
    
        def do_prompt(self, line):
            "Change the interactive prompt"
            self.prompt = line + ': '
    
        def do_EOF(self, line):
            return True
    
        def do_foo(self, line):
            print('Running foo {l}'.format(l=line))
    
        def do_bar(self, line):
            print('Running bar {l}'.format(l=line))
    
    if __name__ == '__main__':
        MyInterpreter().cmdloop()
    

    For more information on how to use the cmd module, see Doug Hellman’s excellent tutorial.


    Running the code above yields a result like this:

    % test.py
    > foo 1
    Running foo 1
    > foo 1 2 3
    Running foo 1 2 3
    > bar 2
    Running bar 2
    > baz 3
    *** Unknown syntax: baz 3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write a C# application calling a command line tool (probably written
I need to write a simple command-line application in Java. It would be nice
I'd like to run MyRunnableJar.jar from command line. It will need to read from
Since I write a command line program to check cpp files, and it need
I need to write a haskell program that retrieves a file from command line
I need to locate a command line tool that would allow me to write
I would like to learn how to write the simplest server/client C++ command line
I have a command line application called MyApp.exe, that I want to use like
I need to automate an command line application. It asks the user to enter
I need to write a Red Hat Linux command line tool that launches a

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.