0

I am making a terminal emulator in Python 3. The commands are being stored in functions, like:

def rd(os_vartmp, os_vartmp2):
    if os_vartmp == None:
        print('rd [path] [-S]')
        print('Delete a folder')
    else:
        if os.path.isfile(os_vartmp) == True:
            if os_vartmp2 == '-S': print('a')
            else:
                print(ERR5)

a = input('Command: ') 

The terminal works like this:

  1. Asks user for input
  2. Splits the input
  3. Uses the first part of input to search a function in locals
  4. If there is one, uses the rest part of input as argument
  5. Calls the function

The thing here is, when i call the function 'rd' with, for example, 'rd "boot.py" -S' it works just fine. But if i need to call it like this: rd "boot.py", it throws me a error about 1 argument given when 2 are required. Is there a fix for that?

HackerGamerLV
  • 37
  • 1
  • 9
  • Have the function accept an arbitrary number of parameters with [`def rd(os_vartmp, *args)`](http://stackoverflow.com/questions/36901/what-does-double-star-and-star-do-for-python-parameters) – TessellatingHeckler May 12 '16 at 00:19

2 Answers2

2

You can make an argument optional by assigning a value in the method definition. For example:

def Add(x=0, y=0):
    return x+y

If you input only one value, y will default to 0. If I wanted to give y a value but have x fall back on it's default value I could do Add(y=10). I hope this helped!

Matthew Faigan
  • 85
  • 1
  • 1
  • 9
1

Have you tried this?

def rd(os_vartmp, os_vartmp2="-S"):

Instead of trying to get null value, which would require rd("boot.py",null), you can ser default value and then you can do rd("boot.py").

Hope it works.

mhyst
  • 297
  • 1
  • 7