argc - print the number of arguments passed. That's it. Seriously, why doesn't this already exist?
argc ARGUMENT...
Prints the number of command-line arguments passed to the program in base 10, followed by a newline.
The traditional --help and --version arguments aren't supported, in order to ensure seamless argument counting.
By definition, argc includes the program name as the first argument (argv[0]).
If you prefer to count only the user-supplied arguments (excluding the program itself), you can easily adjust:
Using Bash arithmetic:
echo $(($(argc "$@") - 1))Or with bc:
bc <<< "$(argc "$@")-1"To build and install argc manually:
You'll need:
- A C compiler (e.g.
gcc,clang) make- Basic POSIX tools (
install,rm, etc.)
Clone the repository, then run:
make
sudo make installThis will:
- Compile the
argcbinary with optimization flags - Install it to
/usr/local/bin/argc - Install the manpage to
/usr/local/share/man/man1/argc.1
To remove it later:
sudo make uninstallTo clean up build artifacts:
make cleanBefore running the tests, make sure the argc binary exists by compiling it:
makeThen, run the tests with:
prove -v t/argc.t
prove -v t/zero.tOr run them all at once:
prove -v t/This is the main test suite. It checks:
- That
argcis executable - That it returns the correct number of arguments for:
- No extra args
- A few args
- A lot of args (up to 100)
This test uses normal shell-style execution and reflects how argc behaves in the real world.
This one’s… special.
It uses low-level syscall magic to launch argc with literally zero arguments—not even argv[0]. This is only possible by bypassing the shell and calling the execve() system call directly.
It's separated from the main suite because:
- It’s a rare, weird edge case
- Behavior is not guaranteed or portable
- It tells us something about how
argc(and your OS) handles broken expectations
After running tests, you can clean up the compiled binary like so:
make cleanThese tests aren’t here because they’re strictly necessary.
They’re here because you deserve to know your 3-line program works with 100 arguments and 0 expectations.
This is free and unencumbered software released into the public domain.
- Even the smallest tool deserves to be well-made.
- Even the most basic function can be shared with pride.
- Even three lines of C can teach someone how real software is built.
No need.