Python command line programming examples recipes

There has recently been a need for python command line utilities, and I have no experience writing cli. Despite everything, I still have to get them out.

My biggest obstacle is the structure of these programs. In addition, the method of receiving and checking input from the user. I end up in very slow cycles, and I just don't think this is the most efficient approach.

Can someone provide links to open cli programs that I can choose to get a little understanding? Or books, textbooks, etc. that I could handle. I dug around, but had little success (my Google skills should be missing).

+3
source share
4 answers

I like baker . You use it like this:

% cat my.py
import baker


@baker.command
def cmd(start, end):
    print '%s %s' % (start, end)


if __name__ == '__main__':
    baker.run()

% python my.py cmd 2010-12-01 2010-12-31
2010-12-01 2010-12-31
+5
source

Random Tips:

  • module optionparseris good for parsing complex options
  • many python modules are indeed cli programs. See There (e.g. see python2.6/json/tool.pywhich you can run with python -m json.tool)

Recommended use

 def main(arguments):
     etc.

 if __name__ == '__main__':
     # only if we are executed rather than imported as a module:
     import sys
     main(sys.argv)

Thus, parts of the yor application can be reused simply by importing

+4
source

python script " ". ?

0

python 2.7 3 , , argparse optparse. , .

if __name__ == '__main__', .

Falmarri CLI - " ", cmd cmd2. , raw_input, , . , .

0

Source: https://habr.com/ru/post/1781340/


All Articles