Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

python cmd module parse multiple arguments with spaces in it

I am trying to write a Python script that will submit my id and filename and retrieve some info based on the filename. The "Cmd" module should prompt me for both an id and filename. However, so far this is the script that I have written. I have to manually type in my id in the script and once I run this script, it prompts me for a filename. Is there any way in which I can make this script to prompt me for both id and filename (and what if the filename contains spaces).

import requests
from cmd import Cmd


class Term(Cmd):

    prompt = "promtp >>> "

    def default(self, args):
        id = "someid"
        resp = requests.post('http://10.10.10.10/login',
                headers={"Content-Type": "application/x-www-form-urlencoded"},
                data={"username": name, "filename": f"{args}"})

    def do_quit(self, args):
        return 1

term = Term()
term.cmdloop()

The part where the filename contains space is a bit tricky.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

When typing your name in the prompt, also type you id, like this:

promtp >>> your_name your_id

The args in the default method is actually a string, so you should change for singular, like arg.

Then, you have to split the string in arg so you can get the two words passed at the command line. Like this:

    def default(self, arg):
        
        args = arg.split()
        name = args[0]
        id = args[1]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading