commit a337234298da4f5028b7dcaa8991b9ff6bce7b5a from: Sylvain Saboua date: Sat Sep 12 07:09:37 2026 UTC introducing u/Schreq's way of parsing options commit - 9b5bb83430f06d79eaa26391f0ad48e175f95fec commit + a337234298da4f5028b7dcaa8991b9ff6bce7b5a blob - 09b1f9c69a4939ede9d7bf416cc58d2f30dfc6e8 blob + 2747e277cbf4592271217faf9ae42a07e43bb7e6 --- nwpg.ksh +++ nwpg.ksh @@ -19,16 +19,18 @@ COUNT_ARGS="$#" integer dir_flag=0 integer lang_flag=0 -#needs to be fixed (option can be passed using -lPython) -if [[ $COUNT_ARGS -eq 1 || $COUNT_ARGS -eq 3 || $COUNT_ARGS -eq 5 ]] -then - eval project_title=\${$#} ; -else - echo $USAGE; - exit 2 ; -fi +main() { -create_project() { + #needs to be fixed (option can be passed using -lPython) + if [[ $COUNT_ARGS -eq 1 || $COUNT_ARGS -eq 3 || $COUNT_ARGS -eq 5 ]] + then + eval project_title=\${$#} ; + else + echo $USAGE; + exit 2 ; + fi + + # Create project dir/files if ! test -d "$PROJECT_FULL_PATH" ; then mkdir -p "$PROJECT_FULL_PATH" else echo "$PROJECT_FULL_PATH already exists" fi @@ -43,6 +45,29 @@ create_project() { echo "$FILE_EXT files created in $PROJECT_FULL_PATH" } +die() { rc=$1; shift; warn "$@"; exit "$rc"; } +warn() { printf '%s: %s\n' "${0##*/}" "$*" >&2; } +usage() { printf 'usage: %s %s\n' "${0##*/}" "$*"; exit; } +version() { printf '%s\n' "$version"; exit; } + +parse_options() { + # Skip the first argument, which is optstring for getopts. + OPTIND=2 + + for o in $1; do unset "opt_${o%:}"; done + + while getopts ":$1" o; do + case $o in + :) die 1 "option requires an argument -- $OPTARG" ;; + \?) die 1 "unknown option -- $OPTARG" ;; + *) eval "opt_$o=\$((opt_$o + 1)) arg_$o=\$OPTARG" ;; + esac + done + + unset o +} + +#obsolete while getopts 'l:d:' opt ; do case $opt in @@ -59,6 +84,7 @@ do done shift $(($OPTIND - 1)) +#needs to be fixed if [ $dir_flag -eq 1 ] then PROJECT_FULL_PATH="$directory/$project_title" ; @@ -71,6 +97,7 @@ then language="$DEFAULT_LANGUAGE" ; fi +#needs to be refactored case "$language" in C | c ) FILE_EXT="c" ;; @@ -92,5 +119,5 @@ case "$language" in exit 2 ;; esac -create_project +main "$@"