Variable in list - universal user interface, sys.argv


Variable in list - universal user interface, sys.argv



I would like to make class, that will be universal for user interface. I would like, to have some form of list, that I will change for another program, to achive diffrent option from user.



I would like to have class variables like self.force in list, but I do not know how to use this place of list like a variable self.force but not like a argument of list (self.option_list[0][0]).



This code will not work, because I don't know how to solve this problem. It schow only, how I would like to solve problem of universal interface. If you use better way to handle sys.argv I'm open to suggestions.


import sys

class UserInterface:
def __init__(self):
self.option_list = [
#[var, usr_name, def_val, choose_val]
[self.force, '-f', False, True]
[self.main_path, '-m', os.getcwd(), '-NEXT_ARG']
[self.send_to_Kindle_dir_name, '-s',
self.main_path+os.sep+'send_to_kindle', '-NEXT_ARG']
[self.display_info, '-p', False, True]
[self.display_help, '--help', False, True]
]
for i in self.option_list:
i[0] = i[2]
for i in range(len(sys.argv)):
for j in self.option_list:
if sys.argv[i] == j[1]:
if j[3] == '-NEXT_ARG':
if len(sys.argv)>i+1:
j[0] = sys.argv[i+1]
else:
j[0] = sys.argv[i]





take a look at argparse
– Michael
2 days ago





The argparse module is great once you get to needing more than really basic command line processing. The typical rule of thumb I have is when you start having optional arguments, and want something to simply stitch together help text, it simply becomes easier to use the lib. Otherwise sys.argv is fine
– sehafoc
2 days ago





@sehafoc Sure! Now what the OP is wanting to achieve (universal interface) really looks like rewriting argparse
– zezollo
2 days ago





Also this article might be of interest.
– zezollo
2 days ago





Thank you very much, that's it, what I was looking for!
– abdarum
yesterday









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

Opening a url is failing in Swift