Making the best CLI programs - Programming On Unix

Users browsing this thread: 3 Guest(s)
seninha
Long time nixers
One tool to make the best CLI programs is to abstract out common usage patterns from different programs. For example, some utilities produce file sizes, either to be read by another program or by a human. Instead of a -h option for different programs to produce human readable values, use a utility, such as z3bra's human, that converts values from its input into human-readable sizes.

Another practice to make good CLI programs is to use idiomatic functions:
  • A double-hyphen (--) on the argument list stops option parsing. Rather than implementing that, use getopt(3), that handles it and much more.
  • When a program in a script writes to stdout, it is hard to guess which program failed when it does not identify itself. Using the err(1) family of functions writes "progname: errno string: comment", a idiomatic error string that identifies the program by its progname (argv[0]).

I made this table for a post on the book club thread.
It lists and describes the types of CLI that can be used in a script. roguelike interface is ignored, because it does not scriptable.

Code:
┌──────────────────────────────────────────────────────────────────────────┐
│ Interface   Read input   Write output   Change environment   Examples    │
├──────────────────────────────────────────────────────────────────────────┤
│ Filter      ✓            ✓              ✗                    cat, grep   │
│ Cantrip     ✗            ✗              ✓                    rm, touch   │
│ Source      ✗            ✓              ✗                    ls, ps      │
│ Sink        ✓            ✗              ✓                    lpr, mail   │
│ Compiler    From file    To file        ✓                    tar, cc,    │
│ ed          From user    To user        ✓                    sh, ed, gdb │
└──────────────────────────────────────────────────────────────────────────┘

I think there are more (sub)categories of command-line interfaces that can be used in a pipeline.

The pretty-printer.
Pretty-printers are a subcategory of filters or sources whose output is not parseable, instead, the output is meant to be read by the user, not by another program. Pretty printers often use the number of $COLUMNS in a terminal to tabulate the output. ls(1) is a pretty-printer: you should not parse the output of ls(1).

The interactive filter.
Interactive filters are a subcategory of filters whose parsing of input and generation of output is done by the user, not programmatically by the utility. Examples are pick, smenu and fzf.

The wrapper.
Wrappers are utilities whose sole purpose is to call another utility. They programmatically set the environment (either environment variables or command-line arguments) for another utility and run them. An example is xargs(1).

The test.
Testers are a subcategory of cantrips or sinks that returns a result as exit status, not on standard output. The script checks the exit status ($?) of a test and proceeds according to its value.


Messages In This Thread
Making the best CLI programs - by venam - 23-05-2016, 02:13 AM
RE: Making the best CLI programs - by jkl - 23-05-2016, 04:49 AM
RE: Making the best CLI programs - by z3bra - 23-05-2016, 10:20 AM
RE: Making the best CLI programs - by jkl - 23-05-2016, 10:51 AM
RE: Making the best CLI programs - by pranomostro - 28-05-2016, 10:50 AM
RE: Making the best CLI programs - by z3bra - 29-05-2016, 10:52 AM
RE: Making the best CLI programs - by josuah - 01-06-2016, 10:40 AM
RE: Making the best CLI programs - by z3bra - 01-06-2016, 01:15 PM
RE: Making the best CLI programs - by z3bra - 02-06-2016, 03:27 AM
RE: Making the best CLI programs - by jaagr - 04-06-2016, 03:10 AM
RE: Making the best CLI programs - by z3bra - 04-06-2016, 05:01 AM
RE: Making the best CLI programs - by venam - 14-09-2016, 01:58 PM
RE: Making the best CLI programs - by jkl - 14-09-2016, 02:03 PM
RE: Making the best CLI programs - by venam - 14-09-2016, 02:05 PM
RE: Making the best CLI programs - by venam - 12-10-2016, 12:52 AM
RE: Making the best CLI programs - by apk - 12-10-2016, 12:34 PM
RE: Making the best CLI programs - by acg - 12-10-2016, 09:57 PM
RE: Making the best CLI programs - by pranomostro - 13-10-2016, 05:18 AM
RE: Making the best CLI programs - by z3bra - 13-10-2016, 08:53 AM
RE: Making the best CLI programs - by pranomostro - 13-10-2016, 02:12 PM
RE: Making the best CLI programs - by josuah - 17-10-2016, 05:16 PM
RE: Making the best CLI programs - by venam - 25-11-2016, 04:07 PM
RE: Making the best CLI programs - by freem - 04-12-2020, 11:27 AM
RE: Making the best CLI programs - by venam - 21-06-2021, 02:32 AM
RE: Making the best CLI programs - by seninha - 21-06-2021, 06:23 AM
RE: Making the best CLI programs - by venam - 21-06-2021, 07:41 AM
RE: Making the best CLI programs - by z3bra - 24-06-2021, 04:59 AM
RE: Making the best CLI programs - by jkl - 12-10-2021, 05:59 AM
RE: Making the best CLI programs - by freem - 15-10-2021, 12:49 PM
RE: Making the best CLI programs - by z3bra - 17-10-2021, 07:54 AM
RE: Making the best CLI programs - by Kohaku - 03-11-2023, 01:18 PM
RE: Making the best CLI programs - by neeasade - 08-11-2023, 02:29 PM
RE: Making the best CLI programs - by jkl - 09-11-2023, 10:54 PM
RE: Making the best CLI programs - by rocx - 28-11-2023, 08:11 PM