Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

NodeJS Minimist inspired shell script for parsing arguments

NotificationsYou must be signed in to change notification settings

zix99/bash-minimist

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status

bash-minimist is a lightweight bash script inspired by the NodeJS moduleminimist.

It is meant to be a very simple and non-invasive argument parser. It will convert any flags or positional arguments into appropriate variables and expose them to your script.

This will fit into your application well 90% of the time for simple things, and once you outgrow it, it should be easy to convert.

Usage

Downloadminimist.sh and place in your script path.

Addsource minimist.sh to your script after your shebang.

Seeminimist.example.sh for a complete example, and the header ofminimist.sh for details.

#!/bin/bashsource minimist.sh# Now my variables are exposed via ARG_# Positional args have been rewritten, and all flags are excluded. So $1, $2, $3, etc will only be positionalif [[$ARG_HELP=='y' ]];thenecho"Someone passed --help"fi

Configuration

At the top ofminimist.sh there are a few configurable items, which you can change inline or set before calling minimist.

For instance, to export any parsed args (rather than just setting locally-scoped variables), you could do:

#!/bin/bashAOPT_DECLARE_FLAGS="-rx"# Readonly, export on 'declare'source minimist.shecho"Now other scripts can access$ARG_"echo"A:$ARG_A"# Single-character flags are case-sensitiveecho"Flag:$ARG_f$ARG_d$ARG_D"echo"Pos1:$1"

Now, you can call the script like the following:

$ minimist.sh --a=123 mypositional# OR$ minimist.sh mypositional --a=123# OR$ minimist.sh mypositional --a=456 -fD

Error checking

The script by itself provides minimal input sanitation. Any non-alphanumeric character will be replaced by a_

If there is still an error after sanitation, an error will be thrown by default (configurable).

All other validation is expected to be done by the script (required variables, valid values, etc).

Error on Unknown Arguments

The script can exit if an argument is unknown, eg.

AOPT_VALID_ARGS=("verbose""v")# Define acceptable argumentssource minimist.sh

./myscript --unknown would throw an error and exit, while--verbose or-v would be accepted.

License

The MIT License (MIT)Copyright (c) 2019 Chris LaPointe

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

NodeJS Minimist inspired shell script for parsing arguments

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp