Before we start writing any code, we want to understand what we are going to build. Since we are going to build a version of an existing tool, why not just look at how it works?

I’m going to use seq from GNU coreutils. Feel free to use the one from NetBSD or any other seq for that matter.

Typing seq --help gives us some usage information:

$ seq --help
Usage: seq [OPTION]... LAST
  or:  seq [OPTION]... FIRST LAST
  or:  seq [OPTION]... FIRST INCREMENT LAST
Print numbers from FIRST to LAST, in steps of INCREMENT.

Mandatory arguments to long options are mandatory for short options too.
  -f, --format=FORMAT      use printf style floating-point FORMAT
  -s, --separator=STRING   use STRING to separate numbers (default: \n)
  -w, --equal-width        equalize width by padding with leading zeroes
      --help        display this help and exit
      --version     output version information and exit

If FIRST or INCREMENT is omitted, it defaults to 1.  That is, an
omitted INCREMENT defaults to 1 even when LAST is smaller than FIRST.
The sequence of numbers ends when the sum of the current number and
INCREMENT would become greater than LAST.
FIRST, INCREMENT, and LAST are interpreted as floating point values.
INCREMENT is usually positive if FIRST is smaller than LAST, and
INCREMENT is usually negative if FIRST is greater than LAST.
INCREMENT must not be 0; none of FIRST, INCREMENT and LAST may be NaN.
FORMAT must be suitable for printing one argument of type 'double';
it defaults to %.PRECf if FIRST, INCREMENT, and LAST are all fixed point
decimal numbers with maximum precision PREC, and to %g otherwise.

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Full documentation <https://www.gnu.org/software/coreutils/seq>
or available locally via: info '(coreutils) seq invocation'

Of course, even more useful is to actually run the command a few times. First we try with some number, like 10.

$ seq 10
1
2
3
4
5
6
7
8
9
10

With echo $? we can see the exit status of the last command. This is also information we need.

$ echo $?
0

This makes sense, since 0 represents a “normal” exit status, by convention. By the way, what is the difference between a rule and a convention?

Let’s also try to pass in -10. This gives us no output. The exit status is still 0.

$ seq -10
$ echo $?
0

What happens if we pass in something that is not a number?

$ seq foo
seq: invalid floating point argument: ‘foo’
Try 'seq --help' for more information.
$ echo $?
1

So that prints out an error message. The exit status is also 1. We take note of every result we get, since this is what we are going to implement.

I’ll let you go ahead and test some other invocations. For each, take not of the output and the exit status. (Different versions of seq may behave slightly different and support different options).

  1. seq
  2. seq 20
  3. seq 5 10
  4. seq 10 5
  5. seq 10 -1 5
  6. seq 0 5 30
  7. seq 30 -5 30
  8. seq 1 0 10
  9. seq 1 1.5 7
  10. seq 1 1.05 7
  11. seq 1 2 3 4
  12. seq -f “%10.5f” 1 10
  13. seq -s: 1 10
  14. seq -w 0 10 100
  15. seq –version
  16. seq –help

Needless to say, we are not going to copy the text from the --help and --version options. Instead we will write our own text. In the case of --version I prefer to just print out the version number and nothing else.

Now that we know what we are going to implement, let’s get started. Even though this is a small project, let’s still treat this as a project and take it one step at a time. We’ll kick off the implementation here