Package: python-ocm
OCM: Object Command MappingI often need to call command line software in Python scripts, usually throughsubprocess, for example:importsubprocesssubprocess.run(['ls','-l','/Users/dev'])The following points of usingsubprocessmake me feel inconvenient:Need to determine the order of the parameters to determine the order of the listNo parameter verificationIt is not convenient to get parameters, for example:['ls','-l','/Users/dev'], I want to get the directoryIn response to the above pain points, I implemented OCM. OCM is named after ORM. When we use Python for database query, we often need to do this:cursor.execute('SELECT xxx FROM xxx WHERE xxx')print(cursor.fetchone()[1])Very inconvenient, you can use ORM to query like this:xxx=XXX.objects.filter(xxx=xxx).first()print(xxx.xxx)So for OCM, what I want to achieve is to run a command like this:ls=LsCommand(is_long=True,directory='.')ls()Installationpip install python-ocmPhilosophyLikeclick, OCM abstracts command-line parameters into two types:Option, can be specified by-l,--long,-o foo.txt, etc.Argument, can only pass the value directly, no keyFor example: inls -l /Users/dev:-lisOption(here there is no value after-l, it is calledflag)/Users/devisArgumentUsagefromocmimportCommand,Option,ArgumentclassLS(Command):is_long=Option('-l',name='is_long',is_flag=True,required=False)directory=Argument(name='directory',required=True)classMeta:exe='ls'ls=LS(is_long=True,directory='/Users/dev')print(ls.is_long)print(ls.directory)returncode,stdout=ls()print(returncode)print(stdout)Parameter TypeOCM built-in parameter types:StringParamTypeIntegerParamTypeFloatParamTypeChoicesParamTypeInstructions:fromocmimportCommand,Option,Argument,IntegerParamTypeclassHead(Command):number=Option('-n',name='number',param_type=IntegerParamType(),required=False)file=Argument(name='file')classMeta:exe='head'head=Head(number=10,file='ocm.py')returncode,stdout=head()Custom parameter type:fromocmimportParamTypeclassMyParamType(ParamType):defconvert(self,value,param,ctx):passdefshow(self,value):passTheconvert methodis responsible for converting the incoming data into the correct data:value, the incoming dataparam, the parameter objectctx, all other incoming parametersTheshow methodis responsible for converting the data into a string to run on the command line:value, the incoming data, generally the result of conversion by the convert methodCallbackThe callback function can perform additional verification and conversion based on the data passed in by the user.fromocmimportCommand,Option,Argument,IntegerParamTypedefadd_one(value,param,ctx):ifvalueisNone:returnNonereturnvalue+1classHead(Command):number=Option('-n',name='number',param_type=IntegerParamType(),required=False,callback=add_one)file=Argument(name='file')classMeta:exe='head'head=Head(number=10,file='ocm.py')returncode,stdout=head()APIOption__init__(self, key, is_flag=False, default=None, param_type=None, required=None, callback=None, multiple=False)parameter:key, the key of theOptionwhen splicing into a command line, for example,-lname, the name of the parameteris_flag, whether this parameter isflag, see the description in the concept section, the default isFalsedefault, the default value of the parameterparam_type, the type of the parameter, see the description of the parameter typerequired, is it necessary?callback, some parameters need to be converted according to the input value, you can customize the callback function, see the description of the callback function sectionmultiple, whether this parameter will be passed in multiple, for example,ls -l /Users/dev /Users/dev/Downloads/,directoryparameter is passed twiceprocess_value(self, value, ctx)Verifyvalueaccording to the rules, and return the verified result:value, the value to be verifiedctx, the value of other parameters passed in at the same time, used for the verification of the parameterArgument__init__(self, is_flag=False, default=None, param_type=None, required=None, callback=None, multiple=False)parameter:name, the name of the parameterdefault, the default value of the parameterparam_type, the type of the parameter, see the description of the parameter typerequired, is it necessary?callback, some parameters need to be converted according to the input value, you can customize the callback function, see the description of the callback function sectionmultiple, whether this parameter will be passed in multiple, for example,ls -l /Users/dev /Users/dev/Downloads/,directoryparameter is passed twiceprocess_value(self, value, ctx)Verifyvalueaccording to the rules, and return the verified result:value, the value to be verifiedctx, the value of other parameters passed in at the same time, used for the verification of the parameter