command line - What are the best-practices for implementing a CLI tool in Perl? -
I am implementing a CLI tool using Perl. What are the best practices we can do here?
As a prelude, I spent 3 years in engineering and a major financial company in Pearl for a lot Applying Complex Command Line ToolsSet The following ideas are basically part of our team's design guidelines.
User interface
-
Command line options: Provide default value as much as possible.
-
Any scalable parameter for any command with more than 2 options
-
The readable option names are if the command line Length is a concern for non-interactive calling (for example, some unnoted heritage sphere has fewer limitations on command lines), provide less aliases - GetOpt :: Long allows easily.
-
At the very least, print all the options - the default value in "Help".
Better yet, print all the options "present" value (for example if a parameter and value are given with "help", the support message will print the value of the parameter from the command line In this way, people can collect command line string for complex commands and can actually verify it by adding "-help" before it runs.
-
Unix Standard Conference Follow non-zero if your The disease can be useful (such as capturing the value / grepping / whatnot) output, so make sure that any error / diagnostic message is sent to STDERR. They can be easily divided.
< Li> -
If there is a side effect of the command, then there is a great idea when the "whatif / no_post" option is .
Ideally, the user "redirects" through input line output parameters - this provides a very simple way for people to build complex pipes using your order Need to It is OK to get an error message - Logfile is the option.
implementation
-
As mentioned earlier, do not invent the wheel again. Use Standard Command Line Parameter Handling Module - MooseX :: Getopt, or Getopt :: Long
-
For Getopt :: Long, all parameters have the same hash as an individual form Resist the variable from Many useful samples include that the CLI argument object to the hash creator.
-
Ensure that your error messages are clear and informative ... e.g. "$!" Include a separate "file not found" vs. "file not readable" errors to spend extra 1 minute and 2 lines in your code in any IO-related error messages, because 30 minutes in the productive emergency Is opposed to spending because a non-readable file error was misused due to the operation Operations "There is no input file" - this is a real life example Land.
-
Actually CLI is not specific, but validates all the parameters, ideally after receiving them. CLI does not allow "front-end" verification such as webapps, so be super super alert.
-
As discussed above, in the other reasons already listed for modifying business logic, as a web app, I reapply an existing CLI tool. It had to do - and it is not difficult that the argument is already properly designed module.
Interesting links
I will try to add more bullets while remembering them.
Comments
Post a Comment