Pregunta

Here's the thing, I want to develop a CLI program (in Python I think) and I'm a really beginner (Also is there a good IDE for Python?) in Python so I would like some of your knowledge to help figure out how to do this.

So for the program I thought of something like that :

<program_name> category1 action1 args

<program_name> category1 action2 args

<program_name> category2 action1 args

Something like openssl with : openssl enc -e -bf-cbc ...

So of course I think the Command Pattern will be usefull but I don't know how to arrange the different categories.

¿Fue útil?

Solución

There are multiple argument parsers available, I will mention mine favourite ones:

  • argparse - built into Python till 2.7, older versions can install it as a package from PyPi
  • plac - package from pypi, based on argparse but much simpler to use.
  • docopt - excellent solution based on writing docstring for the command first and parsing it as specification for the command - this results in shortest and very readable code you can imagine. Must be installed from pypi

By "install from pypi" I mean it can be found at http://pypi.python.org and can be installed by pip command. First thing to do after installing Python is to be sure, pip command is installed. It comes with Python 3.4, for older versions of Python see http://www.pip-installer.org/en/latest/installing.html#install-pip

Personally, I do not use argparse any more, it talks too much and is not very readable.

In any case, grab some tutorials and test them first, this will help you to start quickly:

For plac see: https://github.com/kennethreitz-archive/plac, there is a link to pdf and html documentation, plus github provides set of nice examples.

For docopt see great introduction at http://docopt.org/ and samples at https://github.com/docopt/docopt/tree/master/examples

Otros consejos

I agree with using argparse - but if you are really a beginner - don't use an IDE - get used to the language. If you want a good editor for multi-file programs - used Kate - it isn't an IDE but it can be very useful.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top