Live data from Hacker News

Help Message for Shell Scripts

samizdat.dev

51–60 of 129 posts

Re: Help Message for Shell Scripts

#51

This seems like a neat sed trick, but I'm not sure that it's useful for this particular case? When I write a shell script, I often write a help function if it's not a totally trivial script, but there's no need for this cryptic sed expression, right? You can just call `echo` a few times and do it the obvious way. That works better for maintainability and if you put it at the top of the file then it's immediately visi…

Right, it's a bit useless trick. I guess the author is just exploring how to organize help, maybe thinking about larger scripts or maybe doing it for the article. Either way it's hard to see it going anywhere with sed. If you were to explore parsing and organizing help, I'd suggest starting with a simple pure shell loop like this:

   while read -r line; do
      case "$line" in 
      "###"*) 
         echo "${line#\###}" ;;
      esac
   done 

Re: Help Message for Shell Scripts

#52
post #43

Earlier quoted context omitted.

Note that this doesn't work on macOS, where the builtin `man` command doesn't support the `-l` option.

Ah interesting, is there any workaround for Mac? Otherwise, I may just have to fallback to stripping the man page formatting and sending it to less.

If I understand your need correctly: pandoc.

Re: Help Message for Shell Scripts

#54
post #14

Earlier quoted context omitted.

Thumbs up for 'Click'. I used it for a project once, and I was really happy with it. Easy to use, good docs. Would use it again.

Googling the library appears to be about ~8,000 lines of code (core.py is ~2,000 alone). Is that really reasonable sounding to most people for parsing CLI input/output and display manpages or helptext?

I suppose it depends on the use case. Personally I've always thought argparse is good enough, and have never hit a roadblock "because I'm using argparse" so to say. Having said that, I do like the pattern click is going for. If it argparse allowed the same pattern, in my opinion that would be cool, and it would probably be my first choice.

Re: Help Message for Shell Scripts

#55
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...

Try pyhon click: from flask& jinja2 author, it is very nice

Re: Help Message for Shell Scripts

#56
post #53

Didn't work on macOS (multiple sed errors - switches differ from Linux) but prompted me to write a help function ;-)

I have a project that makes heavy use of sed.. On macOS, I `brew install gnu-sed` and use `gsed` instead, so it works like other platforms.

Re: Help Message for Shell Scripts

#58
post #43

Earlier quoted context omitted.

Ah interesting, is there any workaround for Mac? Otherwise, I may just have to fallback to stripping the man page formatting and sending it to less.

If I understand your need correctly: pandoc.

pandoc is not installed on macOS out-of-the-box though right? The user would have to pull via homebrew or something?

Re: Help Message for Shell Scripts

#59

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 a multiline string:

  #!/bin/bash
  USAGE="my-script — does one thing well
    
    Usage:
      my-script  
    
    Options:
         Input file to read.
        Output file to write. Use '-' for stdout.
      -h        Show this message.
  "

  help() {
    echo "$USAGE"
  }
This is my standard approach which is cleaner for putting the documentation at the very top of the file like the linked article.

Re: Help Message for Shell Scripts

#60
post #54

Earlier quoted context omitted.

Googling the library appears to be about ~8,000 lines of code (core.py is ~2,000 alone). Is that really reasonable sounding to most people for parsing CLI input/output and display manpages or helptext?

I suppose it depends on the use case. Personally I've always thought argparse is good enough, and have never hit a roadblock "because I'm using argparse" so to say. Having said that, I do like the pattern click is going for. If it argparse allowed the same pattern, in my opinion that would be cool, and it would probably be my first choice.

argparse should not be the first thing to reach for, imo, when good old sys.argv can do the job.
Post reply on HN