easyoptions icon indicating copy to clipboard operation
easyoptions copied to clipboard

Allow arbitrary printing of the help text

Open dsl101 opened this issue 10 years ago • 0 comments

My script requires a parameter, but at the moment there seemed no clean way to show the help output instead of the rather pedantic "Parameters required - try --help". This is what I started with:

if [[ ${#arguments[@]} -ne 1 ]]; then
    parse_documentation
    echo "${documentation}"
    exit
fi

I've now added this function to the top of easyoptions, just after the parse_documentation definition:

show_documentation() {
    [[ -z "$documentation" ]] && parse_documentation
    echo "$documentation"
    [[ -z $1 ]] || exit
}

so my user code can now be:

if [[ ${#arguments[@]} -ne 1 ]]; then
    show_documentation andExit
fi

or:

if [[ ${#arguments[@]} -ne 1 ]]; then
    show_documentation
    die "Error: At least 1 argument is required."
fi

Of course, if you pass anything to the show_documentation function, it will exit. I've just been doing a lot of smalltalk programming lately... I also changed the final section of easyoptions to take advantage of that function, so the documentation is only 'shown' in one place to make maintenance easier (e.g. if parse_documentation was to change):

# Help option
if [[ -n "$help" ]]; then
    show_documentation andExit
fi

dsl101 avatar Nov 07 '14 10:11 dsl101