cmdarg icon indicating copy to clipboard operation
cmdarg copied to clipboard

cmdarg is not safe for recursive use (need a way to specify multiple sets of arguments based on first cmdarg_argv ?)

Open akesterson opened this issue 11 years ago • 0 comments

Consider the scenario where a tool has multiple 'subcommands', and in order to enable different sets of options for each 'subcommand', the author makes a cludge like this:

cmdarg_usage_recursing=0
eval "$(declare -f cmdarg_usage | sed s/'^cmdarg_usage'/'original_cmdarg_usage'/g)"                     
function cmdarg_usage                                                                                   
{                                                                                                       
    if [[ $cmdarg_usage_recursing -eq 1 ]]; then                                                        
        original_cmdarg_usage                                                                           
        return                                                                                          
    fi                                                                                                  
    action=${cmdarg_argv[0]}                                                                            
    if [[ "$action" != "-h" ]] && [[ "$action" != "" ]] && [[ "${myprog_actions[$action]}" != "" ]]; then
        cmdarg_usage_recursing=1                                                                        
        myprog_action_$action -h                                                                         
        return                                                                                          
    fi                                                                                                  
    original_cmdarg_usage                                                                               
    return                                                                                              
}                                                                                                       

... The 'cmdarg_usage_recursing' bit is required because, without that check in place, calling into the various action functions (which each implement their own cmdarg_purge / cmdarg calls), resulting in some really unpleasant infinite recursion.

We need a way to make cmdarg_parse recursive safe, or to specify with cmdarg "When cmdarg_argv is empty or -h, use this argument set; when it's set, use this argument set over here".

akesterson avatar May 14 '14 17:05 akesterson