bargs
bargs copied to clipboard
[ENHANCEMENT] Add a predefined prefix to variables
Description
Currently, bargs exports variables UPPER_CASE and lower_case.
I'm using a very strict naming convention, see Writing Bash Scripts Like A Pro - Part 1 - Styling Guide
| Type | Scope | Convention |
|---|---|---|
| Environment | Global | MY_VARIABLE |
| Global | Global | _MY_VARIABLE |
| Local | Function | my_variable |
Using bargs forces me to redefine variables
# Assuming my_global_var was declared in bargs_vars
# MY_GLOBAL_VAR will be available as an environment variable in my script
# So now I need to do this to define it as a "global var"
_MY_GLOBAL_VAR="${MY_GLOBAL_VAR:-"my-default-value"}"
Possible solution
The file bargs_vars contains metadata options for running bargs, so adding options, such as vars_perfix, won't break anything in bargs as it ignores unknown keys.
I'm thinking of adding four options:
-
vars_prefix=""- Defaults to"", adds a prefix to all exported variables.- Enables isolating the app from other predefined environment variables
- Makes it easier to
grepall variables that were exported bybargswith, so I usedvars_prefix=MY_APP_PREFIX_inbargs_vars, I could do the followingenv | grep "MY_APP_PREFIX_" # results MY_APP_PREFIX_GOOGLE_CLIENT_ID MY_APP_PREFIX_GOOGLE_CLIENT_SECRET
-
vars_uppercase="true"- Defaults to "true" which avoids breaking previousbargs.shversions. Setting tofalsewill drop the assignment to UPPERCASED env vars. -
vars_lowercase="true"- Defaults to "true" which avoids breaking previousbargs.shversions. Setting tofalsewill drop the assignment to LOWERCASED env vars. -
vars_type=environment- Defaults to "true" which avoids breaking previousbargs.shversions. An alternative value would beglobal, so the variables are not exported withexport _MY_VAR="my value", but only declared as global variables `_MY_VAR="my value"
A concrete example implementing the above solution
bargs_vars
---
name=person_name
default="Willy"
---
name=age
default=99
---
name=bargs
description=bash example.sh -n Willy --gender male -a 99
vars_uppercase=false
vars_lowercase=false
vars_prefix=_APP_
vars_type=global
default=irrelevant
---
---
Should result with the following variables
_APP_PERSON_NAME="Willy"
_APP_AGE="99"
I need to estimate the effort, though I don't think it's a big deal. Would love to get some feed to see if this type of feature will enhance your experience with bargs.