├── a.sh └── pygetopts /a.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | pyp=$(cat < /dev/null || (echo "$args" && exit) 13 | 14 | [[ ! -z $appr ]] && echo "$appr" 15 | [[ ! -z $show ]] && echo "$show" 16 | [[ ! -z $name ]] && echo "$name" 17 | #echo "$calcf" 18 | -------------------------------------------------------------------------------- /pygetopts: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | 4 | from argparse import * 5 | import sys 6 | 7 | def calci(s): 8 | return str(int(eval(s))) 9 | 10 | def calcf(s): 11 | return str(eval(s)) 12 | 13 | specials = { 14 | "calci" : calci, 15 | "calcf" : calcf 16 | } 17 | 18 | class ArgB: 19 | def __init__(self, line): 20 | if "=" in line: 21 | self.getopts(line) 22 | else: 23 | self.getspecial(line) 24 | 25 | def getopts(self, line): 26 | args = line.strip("\n").split("=") 27 | self.short = f'-{args[0][0]}' 28 | self.long = f'--{args[0]}' 29 | self.type = None 30 | self.action = None 31 | self.help = None 32 | self.choices = None 33 | self.nargs = 1 34 | for i in range(1, len(args), 2): 35 | if i + 1 < len(args): 36 | if args[i] == "type": 37 | if args[i + 1] == "int": 38 | self.type = int 39 | elif args[i + 1] == "str": 40 | self.type = str 41 | elif args[i] == "action": 42 | self.action = args[i + 1] 43 | elif args[i] == "help": 44 | self.help = args[i + 1] 45 | elif args[i] == "nargs": 46 | self.nargs = int(args[i + 1]) 47 | elif args[i] == "choices": 48 | self.choices = args[i + 1].split(",") 49 | 50 | def getspecial(self, line): 51 | line = line.strip("\n") 52 | if line == "calci": 53 | self.short = None 54 | self.long = f'--calci' 55 | self.type = str 56 | self.help = "Calculate integer simple math equation" 57 | elif line == "calcf": 58 | self.short = None 59 | self.long = f'--calcf' 60 | self.type = str 61 | self.help = "Calculate float simple math equation" 62 | 63 | def parseopts(self, parser): 64 | if self.short == None: 65 | parser.add_argument(arg.long, type=arg.type, help=arg.help) 66 | elif self.action != None: 67 | parser.add_argument(arg.short, arg.long, action=arg.action, help=arg.help) 68 | else: 69 | parser.add_argument(arg.short, arg.long, action=arg.action, type=arg.type, choices=arg.choices, nargs=arg.nargs, help=arg.help) 70 | 71 | if __name__ == "__main__": 72 | parser = ArgumentParser() 73 | 74 | for line in sys.stdin: 75 | if line != "\n": 76 | arg = ArgB(line) 77 | arg.parseopts(parser) 78 | else: 79 | break 80 | args = parser.parse_args() 81 | 82 | d = vars(args) 83 | 84 | out = "" 85 | for key, value in d.items(): 86 | if key in specials and value != None: 87 | out += f'{key}="{specials[key](value)}"\n' 88 | elif value != None and value: 89 | if isinstance(value, list): 90 | out += f'{key}="{" ".join(value)}"\n' 91 | else: 92 | out += f'{key}="{value}"\n' 93 | else: 94 | out += f'{key}=\n' 95 | 96 | print(out) 97 | --------------------------------------------------------------------------------