Second, it was chosen for POSIX compliance. Being a built-in, getopts can set shell variables to use for parsing - impossible for getopt or any external program. There are some other advantages to using the getopts built-in. Specifies 1 or more option-argument pairs Specifies 0 or more option-argument pairs Instead, reconsidering your option design will likely produce a cleaner result. Both options require a lot of extra code, extra testing and maintenance cost, and complexity for the user. The first could be implemented in parsing logic with getopts, the second would require using getopt or implementing your own logic. NOTE: Although included in the POSIX spec, mutually exclusive options,, and optional option-arguments, -f and ] are not directly supported by getopts. The IEEE Std 1003.1-2017 (POSIX.1-2017) utility syntax formalizes command line utility syntax which can be summarized as: Note sh’s pluralization of the Gnu facility: getopts vs. Since we always start with design, let’s figure out how to describe how to use our utility to our users - taking our cues from the sh built-in getopts and common command line utilities like grep. The language and framework will provide robust tools for testing and validation Creating shell utilities in Go is simple enough for anyone brave enough to attempt it, especially using a framework like Cobra. When you find needed functionality approaching something like docker, you are much better off using golang and the Cobra library. You are creating an operational and maintenance money pit. You can write a pretty complex bash script that does many “command line application” things, but if you operate at scale, you are not doing anyone any favors. Our scope in a shell script is a “command line utility”. In contrast, a “command line application” like docker or kubectl has global options, single character and long form options with required and optional options, required and optional operands, and sub-commands that may take distinct own options and operands, that overlap with other sub-commands AND may inherit global options AND may propogate their distinct options to other sub-commands. Note that I am using POSIX names option and operand for the older names flags and positional arguments. Even the advanced functionality of grep takes only single character options and option arguments and operands. A command line utility like echo or tr provides a couple of single character options with perhaps some operands. Command line utilitiesĬommand line utilities have a user experience we want to replicate that so our scripts feel like every other command line utility - a polished and familiar presentation.Ī “command line utilty” is very different than a “command line application”. NOTE: You can clone the source here, and use the start_test_env.sh script to setup an Alpine Linux test environment to play along as you read. We’ll apply the same approach to creating a robust utility script template that parses and validates common command line option patterns, produces colored output, and has a common color and validation implementation with testing.Īt the end of this article, you will understand how to design solid command line driven scripts and have templates to use as boilerplate for your own scripts. Last month, we developed a toolbox of basic good software engineering principles in Shell Scripts 1: Bourne back again. OR… on writing a clear, consistent and simple script utiltiy.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |