Live data from Hacker News

Bash code generator for command-line arguments

github.com

21–30 of 54 posts

Re: Bash code generator for command-line arguments

#21
post #20

I start all my bash scripts from the following template, then modify it as needed. It is not fancy, but I think it supports the lowest common denominator that is compatible with other flag processing systems in all other languages that I am aware of. (In particular, it does not support '--option=opt' but uses '--option opt', which is cross-compatible). I like that it is small, easy to maintain, and avoids external de…

In my experience this is the simplest way. The only mod I’d suggest is to put positional arguments into an array so you can put flags anywhere but this will work fine for 99% of scripts.

Re: Bash code generator for command-line arguments

#23
post #20

I start all my bash scripts from the following template, then modify it as needed. It is not fancy, but I think it supports the lowest common denominator that is compatible with other flag processing systems in all other languages that I am aware of. (In particular, it does not support '--option=opt' but uses '--option opt', which is cross-compatible). I like that it is small, easy to maintain, and avoids external de…

This is nice! I suggest writing messages to standard error instead of standard output.

Re: Bash code generator for command-line arguments

#24
Like @bxparks, I have a template for my bash scripts command-line parsing, but I like to stick to GNU-style options and I find that Bash's getopts command handles all these cases without trouble:

command -ffilename -f filename --file=filename -- -f is not an option

My template is at https://gist.github.com/webb/ff380b0eee96dafe1c20c2a136d85ef....

Re: Bash code generator for command-line arguments

#25

Like @bxparks, I have a template for my bash scripts command-line parsing, but I like to stick to GNU-style options and I find that Bash's getopts command handles all these cases without trouble: command -ffilename -f filename --file=filename -- -f is not an option My template is at https://gist.github.com/webb/ff380b0eee96dafe1c20c2a136d85ef... .

Using `-:` to build long-options on top of the short-option-only `getopts` builtin is a great hack.

Re: Bash code generator for command-line arguments

#26
post #25

Like @bxparks, I have a template for my bash scripts command-line parsing, but I like to stick to GNU-style options and I find that Bash's getopts command handles all these cases without trouble: command -ffilename -f filename --file=filename -- -f is not an option My template is at https://gist.github.com/webb/ff380b0eee96dafe1c20c2a136d85ef... .

Using `-:` to build long-options on top of the short-option-only `getopts` builtin is a great hack.

Thanks!

Re: Bash code generator for command-line arguments

#27

I'm probably going to get downvoted for this, but: writing bash scripts in general just isn't a fun experience. years ago, i started just writing ruby scripts for execution in the terminal instead of bash, and haven't had any problems with it so far.

Yes, Bash is nice as long as you use it as a shell only.

For a scripting language, better look elsewhere.

If we had to standardize on one language, perhaps Python would be a good choice.

Re: Bash code generator for command-line arguments

#28
post #20

I start all my bash scripts from the following template, then modify it as needed. It is not fancy, but I think it supports the lowest common denominator that is compatible with other flag processing systems in all other languages that I am aware of. (In particular, it does not support '--option=opt' but uses '--option opt', which is cross-compatible). I like that it is small, easy to maintain, and avoids external de…

This is nice! I suggest writing messages to standard error instead of standard output.

You mean like this?

  echo "message" 1>&2
Yes, that's probably a good idea, though I cannot recall this causing a problem for any script that I've written so far.

Re: Bash code generator for command-line arguments

#29
post #20

I start all my bash scripts from the following template, then modify it as needed. It is not fancy, but I think it supports the lowest common denominator that is compatible with other flag processing systems in all other languages that I am aware of. (In particular, it does not support '--option=opt' but uses '--option opt', which is cross-compatible). I like that it is small, easy to maintain, and avoids external de…

In my experience this is the simplest way. The only mod I’d suggest is to put positional arguments into an array so you can put flags anywhere but this will work fine for 99% of scripts.

The problem I have with bash arrays is that I use them so infrequently that I cannot remember its syntax. I have to look up the manual each time, and it's difficult to find things in the bash man pages.

Re: Bash code generator for command-line arguments

#30

I'm probably going to get downvoted for this, but: writing bash scripts in general just isn't a fun experience. years ago, i started just writing ruby scripts for execution in the terminal instead of bash, and haven't had any problems with it so far.

Ruby is definitely a better tool for this but there are situations where it's not available but bash is. That portability is really the only advantage.
Post reply on HN