2

I'm making my own python CLI and i want to pass only String arguments

import sys
import urllib, json
# from .classmodule import MyClass
# from .funcmodule import my_function
def main():

    args = sys.argv[1:]
    #print('count of args :: {}'.format(len(args)))
    #for arg in args:
     #   print('passed argument :: {}'.format(arg))


    #always returns true even if i don't pass the argument as a "String"
    if(isinstance(args[0], str)): 
        print('JSON Body:')
        url = args[0]
        response = urllib.urlopen(url)
        data = json.loads(response.read())
        print(data)

    # my_function('hello world')
    # my_object = MyClass('Thomas')
    # my_object.say_name()
if __name__ == '__main__':
    main()

I execute it by api "url" and this is the correct output:

enter image description here

Although when i'm trying to execute api url without passing it as a String my output is a little odd:

enter image description here

How can i accept only String arguments?

What I've tried so far:

  • Found this solution here but it didn't work for me (couldn't recognize the join() function)
1

1 Answer 1

3

problem isn't a python issue. It's just that your URL contains a &, and on a linux/unix shell, this asks to run your command in the background (and the data after & is dropped). That explains the [1]+ done output, with your truncated command line.

So you have to quote your argument to avoid it to be interpreted (or use \&). There's no way around this from a Un*x shell (that would work unquoted from a Windows shell for instance)

Sign up to request clarification or add additional context in comments.

3 Comments

there is no way to basically know if the arguments are strings ?
python doesn't even see the rest of the command line. It's a shell issue. python can't help
@PhillAlexakis arguments (sys.argv) are ALWAYS strings

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.