Skip to content

rakus/parseargs

Repository files navigation

Parseargs

A command line option parser for shell scripts.

Build and Test

Parseargs parses given shell script parameters based on a given option definition. Depending on the given options shell code is generated that can be evaluated using the shells eval builtin.

A Simple Example

The following is a fragment for a shell script that supports the options

  • -l & --long to produce some long, more detailed output
  • -o FILE & --out-file FILE to set the file to write to

Additionally multiple input files can be given as arguments.

#!/bin/sh
script_name="$(basename "$0")"
eval "$(parseargs -n "$script_name" -o 'l:long#details,o:out-file=output' )"
if [ -n "$details"] ; then
    echo "Long detailed output requested"
fi
echo "Output file: '$output'"
echo "Arguments: $*"

Parseargs parses the given options and creates shell code to set variables. It also prints error messages and exits the script on unknown options.

The Tutorial explains all features using examples.

Building

Normal cargo commands can be used.

Additional steps are provided via Makefile. Run make help to get help on the available targets.

Prerequisites

Some additional tools are needed for building and testing Parseargs.

Basics

  • cargo get - To extract info from Cargo.toml. (cargo install cargo-get).
  • ShellCheck - Linter for shell code. Used in tests. Install using your package manager. As a shell programmer you should use it anyway.

Documentation

Package Build

  • cargo generate-rpm - Pure Rust RPM builder. (cargo install cargo-generate-rpm)
  • cargo deb - Pure Rust Debian package builder. (cargo install cargo-deb)

TODO

This tool is in an early state and there are areas that need further improvements

  • Handle arguments with invalid UTF-8 chars. Today it just error exits.
  • To many Strings where string slices should be used -- most likely a general Rust newbie problem.