Live data from Hacker News

Help Message for Shell Scripts

samizdat.dev

71–80 of 129 posts

Re: Help Message for Shell Scripts

#71

You can also use a "here document" help() { cat Options: Input file to read. Output file to write. Use '-' for stdout. -h Show this message. EOH } If the indentation bugs you, you can use a simpler sed trick to remove leading space so that you can indent it as desired: help() { sed -e 's/ //' Options: Input file to read. Output file to write. Use '-' for stdout. -h Show this message. EOH }

Or just write it in Python.

Re: Help Message for Shell Scripts

#72
post #4

Hmm: % sed -rn 's/^### ?//;T;p' testfile sed: 1: "s/^### ?//;T;p": invalid command code T Looks like it might need GNU Sed or something. But honestly if I want to read the top of the file less works just as well.

Yeah, "man sed" on my machine says, "This is a GNU extension." You could do the same thing with awk instead: awk '{ if (sub("^### ?", "")) { print; } else { exit; } }'

[deleted]

Re: Help Message for Shell Scripts

#73
There's an endless variation on how shell scripts can present help information. Here's another, consider this array:

    ARGUMENTS+=(
      "a,arch,Target operating system architecture (amd64)"
      "b,build,Suppress building application"
      "o,os,Target operating system (linux, windows, mac)"
      "u,update,Java update version number (${ARG_JRE_UPDATE})"
      "v,version,Full Java version (${ARG_JRE_VERSION})"
    )
The lines are machine-readable and alignment is computed by the template:

https://github.com/DaveJarvis/scrivenvar/blob/master/build-t...

When install script[0] help is requested, the following is produced:

    $ ./installer -h
    Usage: installer [OPTIONS...]

      -V, --verbose  Log messages while processing
      -h, --help     Show this help message then exit
      -a, --arch     Target operating system architecture (amd64)
      -b, --build    Suppress building application
      -o, --os       Target operating system (linux, windows, mac)
      -u, --update   Java update version number (8)
      -v, --version  Full Java version (14.0.1)
Using an array reduces some duplication, though more can be eliminated. Scripts typically have two places where the arguments are referenced: help and switch statements. The switch statements resemble:

https://github.com/DaveJarvis/scrivenvar/blob/master/install...

Usually parsing arguments entails either assigning a variable or (not) performing an action later. Introducing another convention would allow hoisting the switch statement out of the installer script and into the template. Off the cuff, this could resemble:

    ARGUMENTS+=(
      "ARG_ARCH,a,arch,Target operating system architecture (amd64)"
      "do_build=noop,b,build,Suppress building application"
      "ARG_JRE_OS,o,os,Target operating system (linux, windows, mac)"
      "ARG_JRE_UPDATE,u,update,Java update version number (${ARG_JRE_UPDATE})"
      "ARG_JRE_VERSION,v,version,Full Java version (${ARG_JRE_VERSION})"
    )
The instructions to execute when arguments are parsed are thus associated with the arguments themselves, in a quasi-FP style. This approach, not including the FP convention, is discussed at length in my Typesetting Markdown series[1].

[0]: https://github.com/DaveJarvis/scrivenvar/blob/master/install...

[1]: https://dave.autonoma.ca/blog/2019/05/22/typesetting-markdow...

Re: Help Message for Shell Scripts

#76

You can also use a "here document" help() { cat Options: Input file to read. Output file to write. Use '-' for stdout. -h Show this message. EOH } If the indentation bugs you, you can use a simpler sed trick to remove leading space so that you can indent it as desired: help() { sed -e 's/ //' Options: Input file to read. Output file to write. Use '-' for stdout. -h Show this message. EOH }

Or just write it in Python.

Python is terrible at one liners. The sed is much more compact.

Re: Help Message for Shell Scripts

#77
post #75

Even better: Don’t use bash. I started using Python instead of bash. It’s way better to read and more maintainable. If I need the performance of native-Unix commands, I can still use them using subprocess.

bash is everywhere

So is python

Re: Help Message for Shell Scripts

#80
post #40
post #7

Handling of arguments is one of the reasons I reach for Python or Powershell instead of a bash script when writing my own stuff. https://docs.python.org/3/library/argparse.html is great. Powershell has the Param keyword that functions like argparse in Python https://docs.microsoft.com/en-us/powershell/module/microsoft...

I also really like the "flag" package for Go, it generates the help text for you and easily lets you set defaults as well as helps you type-check inputs.

I like https://github.com/jpillora/opts for Go arg parsing. It has better, sane defaults with long/short opts.
Post reply on HN