Python creation commands make it a learning curve and a programming language. experienced programmers may even master the basic knowledge of Python within one day, but they can get started with it in just one week, programming Language experts are certain.
With built-in help menus, error handling, and option processing. For some strange reasons, many people do not know Python.®The standard library has all the tools required to create * NIX command line tools with extremely powerful functions. It can be said that Python is the best language for * NIX command line tools, because it works in the philosophical way of "batteries-supported" and emphasizes providing readable code.
But as a reminder, these ideas are dangerous and your life may be messy when you discover how easy it is to create a command line tool using Python. As far as I know, I have not published a detailed article about using Python to create a command line tool.
So I hope you like this article. The optparse module in the Python standard library can complete most of the trivial work of creating the command line tool. Optparse is included in Python 2.3, so this module will be included in many * NIX operating systems.
For some reason, the operating system you use does not contain the required modules. Fortunately, the latest version of Python has been tested and compiled into almost any * NIX operating system. Python supports the following systems: IBM®AIX®, HP-UX, Solaris, Free BSD, Red Hat Linux®Ubuntu, OS X, IRIX, and even several Nokia mobile phones.
The first step in writing excellent command line tools is to define the problems to be solved. This is critical to the success of your tool. This is equally important for solving the problem in the simplest way possible. KISSKeep It Simple Stupid is clearly used here to keep It Simple. Options and other features are added only after the planned functions are implemented and tested.
First, we start by creating the Hello World command line tool. According to the above suggestions, we use the simplest possible terms to define the problem. Problem definition: I want to create a command line tool that prints Hello World by default and provides the option to print the name of the person who cannot get through. Based on the preceding instructions, you can provide a solution that contains a small amount of code.
Hello World command line interface (CLI ):
- #!/usr/bin/env python
- import optparse
- def main():
- p = optparse.OptionParser()
- p.add_option('--person', '-p',
- default="world")
- options, arguments = p.parse_args() print 'Hello %s' % options.person
- if __name__ == '__main__':
- main()
However, we can do more than this by creating a few PPython commands. We can get the Help menu automatically generated. Here, we finally see the platform relevance of the multithreading mechanism in Python. Under the Python25 \ Python directory, there are a large number of files such as thread _ ***. h in these files.
The native threads of different operating systems are encapsulated and exposed to Python through a unified interface. For example, the PyThread_allocate_lock here is such an interface. The thread_nt.h here is packaged with the native thread of the Win32 platform. In the code analysis later in this chapter, there will be a lot of platform-related code. We take the Win32 platform as an example.