Supercharge Your Zsh Scripts with CMDS-Framework: Effortless Argument Parsing and More!
Tired of wrestling with clunky shell scripts? Do you dream of elegant argument parsing and auto-completion without sacrificing the speed and flexibility of shell scripting? Then CMDS-Framework (cmds) is your answer. This Zsh framework lets you build robust, user-friendly scripts with minimal effort. Keep reading to discover how cmds can revolutionize your shell scripting workflow.
Why Choose CMDS-Framework?
Shell scripts are great for quick system integration, but they often lack key features like argument parsing and completion. While proper programming languages offer solutions, they sacrifice the "hackability" of shell scripts. CMDS-Framework bridges this gap, offering the best of both worlds.
Here’s how CMDS-Framework solves common scripting headaches:
- Automated Argument Parsing: Say goodbye to manual parsing! Define your arguments, and cmds does the rest.
- Effortless Tab Completion: Auto-completion for commands, flags, and arguments – all automatically generated.
- Auto-Generated Descriptions: Keep your scripts organized with auto generated command help menus, improving your script's accessibility.
- Clean, Organized Structure: Enforce a consistent structure for easy maintenance and collaboration.
CMDS-Framework in Action: A Quick Start
Let's walk through building a simple script using CMDS-Framework.
-
Source the Library: Add
source .lib.sh
to your.zshrc
file. -
Create a Subcommand Directory: Inside your cmds directory, create a new folder. This becomes your subcommand.
-
Add a Description: Create a
.description
file within the new directory: -
Create a Command Script: Add a script (e.g.,
mycommand.sh
) inside the subcommand directory:
Now, running cmds new mycommand.sh --input mydata.txt --verbose
will automatically parse the arguments, pass them to your script, and execute it.
Writing Robust Scripts: The Core Components
CMDS-Framework enforces a simple yet effective structure. Every script needs these three variables defined, and an execute function.
desc
: A one-line description of the script.args
: An array defining your script's arguments. The format is:--{flag}:[options]{description}
. Options can beo
(optional) orb
(boolean).help
: An array containing the script's name and a longer, more detailed description.execute()
: The function where your script's logic resides. Arguments are automatically converted to global variables that can be utilized in your shell scripts.
Mastering the args
Array: Argument Definition Demystified
The args
array is the heart of CMDS-Framework's argument parsing. Here's a breakdown:
--flag
: The name of the flag (must start with--
). The flag is automatically converted into a Bash variable name.[options]
:o
: Makes the argument optional.b
: Makes the argument a boolean (no value required).
description
: A short explanation of the argument.
Example: args=( "--name:The user's name" "--verbose:[b,o]Enable verbose mode")
Effortless Argument Forwarding
Sometimes, you need to pass arguments directly to another command within your script. CMDS-Framework makes this easy with argument forwarding. Anything after --
is put into the $forwarded
array.
Example:
Now, cmds myscript -- get pods -n mynamespace
will execute kubectl get pods -n mynamespace
.
Inside the CMDS-Framework Engine
The secret sauce of CMDS-Framework lies in the .lib.sh
file. Sourcing this file provides several components:
- Variables: Useful variables for your scripts, like
CMDS_DIR
. - Logging Functions:
lib_error
,lib_info
, andlib_warning
for consistent logging. - Output Helpers: Functions for generating consistent help messages and descriptions.
- Argument Parser: Handles argument validation and variable creation.
- Command Runner (
cmds
function): Locates the script, parses arguments, and executes the script'sexecute
function in a subshell, ensuring your interactive session stays pristine.
Embrace the Magic of Sourcing
CMDS-Framework relies heavily on the source
command. When .lib.sh
sources a script, it loads the script's variables into its environment. This allows your execute
function to access variables defined in both the script and .lib.sh
, creating a seamless and intuitive scripting experience.
Unlock Your Shell Scripting Potential with CMDS-Framework
CMDS-Framework empowers you to write cleaner, more maintainable, and more user-friendly shell scripts. By handling argument parsing, completion, and validation automatically, it frees you to focus on the core logic of your scripts. Say goodbye to tedious boilerplate and hello to a new era of shell scripting efficiency! Start harnessing the power of CMDS-Framework today and transform your command‑line workflow.