@@ -1123,6 +1123,11 @@ argsparse_usage_long() {
11231123 " ${properties[$property]} " " ${values[*]} "
11241124 fi
11251125 done
1126+ if argsparse_are_environment_variables_enabled
1127+ then
1128+ printf " ${bol} Environment variable: %s\n" \
1129+ " $( argsparse_option_environment_variable_name " $long " ) "
1130+ fi
11261131 done
11271132}
11281133
@@ -1380,6 +1385,146 @@ argsparse_maximum_parameters() {
13801385}
13811386
13821387
1388+ # # @var String __argsparse_environment_variable_prefix
1389+ # # @brief The prefix for option-setting environment variables
1390+ # # @private
1391+ # # @ingroup ArgsparseOptionSetter
1392+ declare __argsparse_environment_variable_prefix
1393+
1394+
1395+ # # @fn argsparse_use_environment_variables()
1396+ # # @brief Enable option setting through environment variables.
1397+ # # @details
1398+ # # Once enabled, it cannot be disabled.
1399+ # # @param prefix an environment variable prefix
1400+ # # @ingroup ArgsparseOptionSetter
1401+ argsparse_use_environment_variables () {
1402+ local prefix
1403+ case $# in
1404+ 0)
1405+ prefix=$( argsparse_option_to_identifier " ${argsparse_pgm% .sh} " )
1406+ prefix=${prefix^^}
1407+ ;;
1408+ 1)
1409+ prefix=$1
1410+ ;;
1411+ * )
1412+ return 1
1413+ esac
1414+ __argsparse_environment_variable_prefix=$prefix
1415+ }
1416+
1417+
1418+ # # @fn argsparse_are_environment_variables_enabled()
1419+ # # @brief Tell if option setting through environment variables is
1420+ # # enabled or not.
1421+ # # @details
1422+ # # @retval 0 if argsparse_parse_options would also retrieve option
1423+ # # values from environment variables.
1424+ # # @ingroup @ArgsparseOptionSetter
1425+ argsparse_are_environment_variables_enabled () {
1426+ [[ ${__argsparse_environment_variable_prefix:- } ]]
1427+ }
1428+
1429+
1430+ # # @fn argsparse_option_environment_variable_name()
1431+ # # @brief Give the environment variable name linked to an option
1432+ # # @details
1433+ # # Return the name of the environment variable name that can be used
1434+ # # to set a given option. Works whether option setting through
1435+ # # environment variables is enabled or not.
1436+ # # @param option an option name
1437+ # # @return the environment variable name linked to the option
1438+ argsparse_option_environment_variable_name () {
1439+ [[ $# -eq 1 ]] || return 1
1440+ local option=$1
1441+ local identifier=$( argsparse_option_to_identifier " $option " )
1442+ printf %s_%s " $__argsparse_environment_variable_prefix " " ${identifier^^} "
1443+ }
1444+
1445+
1446+ # # @fn argsparse_option_environment_variable()
1447+ # # @brief Give the content of the environment variable that can be
1448+ # # used to set a given option.
1449+ # # @details
1450+ # # Does not check if option setting through environment variable is
1451+ # # enabled or not.
1452+ # # @param option An option name
1453+ # # @return The content of the environment variable that matches the
1454+ # # given option.
1455+ # # @retval 0 if the variable that matches the given option actually
1456+ # # exists (even if its value is empty)
1457+ argsparse_option_environment_variable () {
1458+ [[ $# -eq 1 ]] || return 1
1459+ local option=$1
1460+ local var=$( argsparse_option_environment_variable_name " $option " )
1461+ if [[ -v $var ]]; then
1462+ printf %s " ${! var} "
1463+ return 0
1464+ fi
1465+ return 1
1466+ }
1467+
1468+
1469+ # # @fn argsparse_set_option_from_environment_value()
1470+ # # @brief Give a value to an option, from environment variable value.
1471+ # # @details
1472+ # # Options can be set from environment variables, and in some cases,
1473+ # # those values have special meanings:
1474+ # # - 0, "false", "no" and the empty string are explicit values to
1475+ # # disable a non-valued option
1476+ # # - (probably more to come)
1477+ # # .
1478+ # # @param option an option name
1479+ # # @param value the value, as found in the process environment, to set
1480+ # # the option to.
1481+ argsparse_set_option_from_environment_value () {
1482+ [[ $# -eq 2 ]] || return 1
1483+ local option=$1
1484+ local value=$2
1485+ if ! argsparse_has_option_property " $option " value
1486+ then
1487+ if [[ $value != + (0| false| no| " " ) ]]
1488+ then
1489+ argsparse_set_option " $option "
1490+ fi
1491+ # There should be an elif statement here, if we want to consider
1492+ # cumulated options differently.
1493+ else
1494+ argsparse_set_option " $option " " $value "
1495+ fi
1496+ }
1497+
1498+
1499+ # # @fn __argsparse_parse_options_env_and_defaults()
1500+ # # @brief set options from environment and from default values.
1501+ # # @details
1502+ # # For every option that have not been set from command line, look for
1503+ # # a matching environment variable (when
1504+ # # argsparse_are_environment_variables_enabled() is true) and apply
1505+ # # default values right after if still left unset.
1506+ # # @private
1507+ __argsparse_parse_options_env_and_defaults () {
1508+ local option value
1509+ for option in " ${! __argsparse_options_descriptions[@]} "
1510+ do
1511+ if ! argsparse_is_option_set " $option "
1512+ then
1513+ if argsparse_are_environment_variables_enabled &&
1514+ value=$( argsparse_option_environment_variable " $option " )
1515+ then
1516+ argsparse_set_option_from_environment_value " $option " " $value "
1517+ elif __argsparse_has_array_item \
1518+ __argsparse_options_default_values " $option "
1519+ then
1520+ argsparse_set_option " $option " \
1521+ " ${__argsparse_options_default_values[$option]} "
1522+ fi
1523+ fi
1524+ done
1525+ }
1526+
1527+
13831528# # @var AssociativeArray program_options
13841529# # @brief Options values.
13851530# # @details
@@ -1651,15 +1796,8 @@ __argsparse_parse_options_no_usage() {
16511796 # Save program parameters in array
16521797 program_params=( " $@ " )
16531798
1654- # Apply default values here
1655- for option in " ${! __argsparse_options_default_values[@]} "
1656- do
1657- if ! argsparse_is_option_set " $option "
1658- then
1659- argsparse_set_option " $option " \
1660- " ${__argsparse_options_default_values[$option]} "
1661- fi
1662- done
1799+ # Retrieve values from environment and set matching options
1800+ __argsparse_parse_options_env_and_defaults
16631801
16641802 # If some mandatory option have been omited by the user, then
16651803 # print some error, and invoke usage.
0 commit comments