-1

I have a basic question about how Python(3) works.

I have a python script which I run with an argument. This argument can have a lot of different values.

This looks as follows: python script.py argument

Inside the script I have a command that looks as follows: command.a(some parameters).

Right now if I want to run command.b(some parameters) or command.d(some parameters) I need to go to the script, change the command.a to command.b or command.d.

I want my script to be more flexible so if I run python script.py argument that inside the script command.argument(some parameters) gets executed and I no longer need to change the value myself before running it.

How would one achieve this in Python? I'm not sure if I explained this clearly.

user2357112
  • 260,549
  • 28
  • 431
  • 505
Stuffooh
  • 11
  • 5
  • Can you please elaborate on it a bit more using proper examples? – Mayank Porwal Apr 10 '20 at 06:30
  • @MayankPorwal I tried to rephrase my question. Hope it is a little more clear. – Stuffooh Apr 10 '20 at 06:37
  • @Stuffooh I've added an answer. Is this what you're trying to do? – Thaer A Apr 10 '20 at 06:48
  • Have you already written code that takes a command line argument? If so your question just boils down to how to use that argument to run a function/method of the same name within your code. Correct? – Simon Notley Apr 10 '20 at 07:06
  • @ThaerA I have edited my question in hope to clarify it. I am not very comfortable yet with python terminology. – Stuffooh Apr 10 '20 at 07:34
  • @SimonN I have edited my question in hope to clarify it. I am not very comfortable yet with python terminology. – Stuffooh Apr 10 '20 at 07:34
  • @Stuffooh Can you share your code so far? – Thaer A Apr 10 '20 at 07:38
  • That was a pretty major edit. I'm not sure the new version is similar enough to the original to actually be a new version rather than a whole new question - at first, I thought you were abusing the edit system to bypass rate limits and ask a new, entirely unrelated question. – user2357112 Apr 10 '20 at 07:41
  • Anyway, https://stackoverflow.com/questions/2612610/how-to-access-object-attribute-given-string-corresponding-to-name-of-that-attrib – user2357112 Apr 10 '20 at 07:41
  • @ThaerA the code consist out of a few thousand lines spread over multiple scripts not sure how to post the code in a clear way. – Stuffooh Apr 10 '20 at 08:06
  • @user2357112supportsMonica I'm sorry I did not mean to abuse the system or break any rules. I'm new to both Python and stackoverflow so struggled writing my question down properly for other people to understand. – Stuffooh Apr 10 '20 at 08:06

2 Answers2

1

Sorry I didn't understand your question :D For this case you can use an input to specify what do you need:

type=int(input("What Type Of Def You Want To Use? "))

And then you can put an IF for you selection:

if(type==1): command.a(args)

elif(type==2): command.b(args)

elif(type==3): command.c(args)

else: print("Invalid Command.. Use:[1,2,3]")

Hope it works for you this time :D

  • Thanks, I think I will use your if statement structure to solve my problem. It is not ideal but don't see any other way to fix it. – Stuffooh Apr 10 '20 at 08:07
  • Np. I just wanted to say it in a simple way. U can use ur creativity for my case too like put it in a (while) or etc.Also you can use input keyboard instead of a variable.. like when u click (A) on ur keyboard.. the program will notice ur option.. Just tried to do my best and hope helped. Sorry again for the first answer :D – Pooya Salehi Apr 10 '20 at 08:17
-1

You can use input:

name = input ("What's your name")
print ("Hello, ", name )

If you're writing a command line tool, it's very doable with the click package. See their Hello World example:

import click

@click.command()
@click.option('--count', default=1, help='Number of greetings.')
@click.option('--name', prompt='Your name',
              help='The person to greet.')
def hello(count, name):
    """Simple program that greets NAME for a total of COUNT times."""
    for x in range(count):
        click.echo('Hello %s!' % name)

if __name__ == '__main__':
    hello()

Running python hello.py --count=3 will give you the output below:

Your name: John
Hello John!
Hello John!
Hello John!
Thaer A
  • 2,243
  • 1
  • 10
  • 14