#!/bin/sh -eu # Create a manpage from the help/version output of an executable/script # Version: 2.0.0 # Copyright: 2014, Koalephant Co., Ltd # Author: Stephen Reay , Koalephant Packaging Team # Create a manpage from the help/version output of an executable/script # # Input: # $1 - the executable or script # # Output: # The rendered manpage, unless `--output` is given proc k_help2man { var KOALEPHANT_TOOL_VERSION = '"2.0.0'" var KOALEPHANT_TOOL_DESCRIPTION = '"Create a manpage from the help/version output of a executable/script'" var KOALEPHANT_TOOL_OPTIONS = $[cat << """ Options: -h, --help show this help -H, --help-command command set the help argument/option to use -n, --name name set the name to use for the tool -N, --alt-name name set an alternate name for the tool -o, --output file set the filename to output to (defaults to stdout) -s, --section section set the manpage section -S, --source source set the program source (i.e. a company/organisation or project/package name) -v, --version show the version -V, --version-command command set the version argument/option to use """ ] var KOALEPHANT_TOOL_ARGUMENTS = '"executable'" source ./base.lib.sh source ./string.lib.sh source ./bool.lib.sh source ./fs.lib.sh var helpCommand = '"--help'" var description = ''"" var section = '"1'" var source = ''"" var versionCommand = '"--version'" var output = '/dev/stdout' var changeDir = 'false' var alternativeName = ''"" while test $Argc -gt 0 { match $1 { with -h|--help k_usage exit with -c|--cd set changeDir = 'true' shift with -H|--help-command set helpCommand = $[k_option_requires_arg @Argv] shift 2 with -d|--description set description = $[k_option_requires_arg @Argv] shift 2 with -N|--alt-name set alternativeName = $[k_option_requires_arg @Argv] shift 2 with -o|--output set output = $2 shift 2 with -s|--section set section = $[k_option_requires_arg @Argv] shift 2 with -S|--source set source = $[k_option_requires_arg @Argv] shift 2 with -v|--version k_version exit with -V|--version-command set versionCommand = $[k_option_requires_arg @Argv] shift 2 with -* echo "Unknown option: $(1)" > !2 k_usage exit 1 with -- shift break with * break } } var executable = $[k_fs_resolve $(1)] var executableName = $[k_fs_basename $(executable) .sh] var dir = $[k_fs_dirname $(executable)] var includeFile = ""$(dir)/$(executableName).$(section).man"" var tmpIncludeFile = $[k_fs_temp_file] if test -z $(executable) { echo "Executable ($(executable)) not provided"; k_usage; exit 1 } elif test ! -f $(executable) || test ! -x $(executable) { echo "Executable ($(executable)) not an executable file"; k_usage; exit 1 } if test $(changeDir) = true { # Adjust output if file cd $(dir) set executable = ""./$[k_fs_basename $(executable)]"" } if test -z $(description) { set description = $[$(executable) $(helpCommand) | sed -E -n -e '/^\s*(Usage|\s*or):/!p' | head -n 1] } printf "[NAME]\n%s - %s\n\n" $[k_string_join ", " $(executableName) $(alternativeName)] $(description) > $(tmpIncludeFile) if test -f $(includeFile) { cat $(includeFile) >> $(tmpIncludeFile) } help2man \ --section $(section) \ --no-info \ --include $(tmpIncludeFile) \ --help-option $(helpCommand) \ --version-option $(versionCommand) \ --source $(source) $(executable) > $(output) } k_help2man @Argv