In a way to standardise commands this
approach uses a config file approach , which contains the system
commands . usage:
The user creates a command file which
lists commands with parameters as %s. a typical command file contains
the following lines for a copy command.
CP_CMD=cp %s %s
now when I call the create_command
function
cmd_str=create_command("/dir1","/dir2","CP_CMD")
the cmd_str contains cp /dir1 /dir2 , which can be eventually
executed.
The command file can contain hundreds
of commands listed one below the other as follows
CP_CMD=cp %s %s
FTP_CMD=ftp %s
DIR_CMD=dir %s
This is just a code snippet elaborating the idea. Adding features like exceptions, Fancier formatting , Error redirection etc is left to you !
""" This Function loads a command file into memory as a dictonary , With Commands as keys"""
def load_command_file(file_name):
command_dict={}
if file_name.isalpha():
print("Error!!command file name should be in ascii\n");
return command_dict
file=open(file_name,'r')
file_lines=file.readlines()
for line in file_lines:
command_list=line.strip().split("=")
command_dict[command_list[0].strip()]=command_list[1].strip()
file.close()
return command_dict
########################################################################################################
""" This function uses the above mentioned methods to create commands based on passed args"""
def create_command(*args):
command_dict=load_command_file(r"C:\Documents and Settings\Raj\Desktop\command_file.txt")
#print(command_dict)
arg_len=len(args)
command_str=command_dict[args[arg_len-1]]
if command_str.count('%s')!=arg_len-1:
print("Error mismatch in args passed and args actually needed")
sys.exit()
command=command_str%(args[:-1])
return command


