bargs icon indicating copy to clipboard operation
bargs copied to clipboard

[ENHANCEMENT] Add a predefined prefix to variables

Open unfor19 opened this issue 4 years ago • 0 comments

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

TypeScopeConvention
EnvironmentGlobalMY_VARIABLE
GlobalGlobal_MY_VARIABLE
LocalFunctionmy_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:

  1. vars_prefix="" - Defaults to "", adds a prefix to all exported variables.
    • Enables isolating the app from other predefined environment variables
    • Makes it easier to grep all variables that were exported by bargs with, so I used vars_prefix=MY_APP_PREFIX_ in bargs_vars, I could do the following
      env | grep "MY_APP_PREFIX_"
      # results
      MY_APP_PREFIX_GOOGLE_CLIENT_ID
      MY_APP_PREFIX_GOOGLE_CLIENT_SECRET
      
  2. vars_uppercase="true" - Defaults to "true" which avoids breaking previous bargs.sh versions. Setting to false will drop the assignment to UPPERCASED env vars.
  3. vars_lowercase="true" - Defaults to "true" which avoids breaking previous bargs.sh versions. Setting to false will drop the assignment to LOWERCASED env vars.
  4. vars_type=environment - Defaults to "true" which avoids breaking previous bargs.sh versions. An alternative value would be global, so the variables are not exported with export _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.

unfor19 avatar Dec 03 '21 10:12 unfor19