Building a CLI Tool
Practical example.
Building a CLI Tool is a free C Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Putting it together
Now we combine arguments, options, and parsing into a small but complete CLI tool.
Our example is a number-formatting utility that reads a value and a few options, then prints a result.
#include <stdio.h>
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("Usage: %s <number>\n", argv[0]);
return 1;
}
printf("You passed: %s\n", argv[1]);
return 0;
}Designing the interface
Good CLI tools define their interface up front. Ours will accept:
-vverbose output-r Nrepeat the output N times- one positional argument: the text to print
#include <stdio.h>
int main(void) {
printf("Interface: prog [-v] [-r N] <text>\n");
return 0;
}Declaring settings
Hold each configurable behavior in a variable with a sensible default. This keeps parsing separate from the actual work.
#include <stdio.h>
int main(void) {
int verbose = 0;
int repeat = 1;
const char *text = NULL;
printf("defaults: verbose=%d repeat=%d\n", verbose, repeat);
return 0;
}Parsing with getopt
Use getopt to fill in the flags. -r takes a value, so its optstring entry is r:.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
int verbose = 0, repeat = 1, opt;
while ((opt = getopt(argc, argv, "vr:")) != -1) {
if (opt == 'v') verbose = 1;
else if (opt == 'r') repeat = atoi(optarg);
}
printf("verbose=%d repeat=%d\n", verbose, repeat);
return 0;
}Reading the positional argument
After getopt, optind points to the first non-option argument. We grab the text from there.
If none is present, we print usage and exit with an error code.
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
while (getopt(argc, argv, "vr:") != -1) { }
if (optind >= argc) {
printf("Error: missing text argument\n");
return 1;
}
printf("text = %s\n", argv[optind]);
return 0;
}Validating numeric input
A robust tool checks that -r received a sensible number. Reject zero or negative repeat counts.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
int repeat = 1, opt;
while ((opt = getopt(argc, argv, "r:")) != -1) {
if (opt == 'r') repeat = atoi(optarg);
}
if (repeat < 1) {
printf("Error: repeat must be >= 1\n");
return 1;
}
printf("repeat = %d\n", repeat);
return 0;
}Doing the work
With settings parsed and validated, perform the actual task. Here we print the text repeat times.
#include <stdio.h>
int main(void) {
const char *text = "hello";
int repeat = 3;
for (int i = 0; i < repeat; i++) {
printf("%s\n", text);
}
return 0;
}Verbose output
The verbose flag can add diagnostic information. Guard such prints behind the flag so normal output stays clean.
#include <stdio.h>
int main(void) {
int verbose = 1;
const char *text = "hi";
if (verbose) printf("[debug] about to print text\n");
printf("%s\n", text);
return 0;
}The complete tool
Here is the full program tying every piece together: parse, validate, then run.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
int verbose = 0, repeat = 1, opt;
while ((opt = getopt(argc, argv, "vr:")) != -1) {
if (opt == 'v') verbose = 1;
else if (opt == 'r') repeat = atoi(optarg);
}
if (optind >= argc) { printf("Usage: prog [-v] [-r N] <text>\n"); return 1; }
const char *text = argv[optind];
if (verbose) printf("[debug] repeat=%d\n", repeat);
for (int i = 0; i < repeat; i++) printf("%s\n", text);
return 0;
}Exit codes matter
CLI tools communicate success through their exit code. Return 0 on success and non-zero on errors.
Scripts and other programs rely on this to decide what to do next.
#include <stdio.h>
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("failure\n");
return 2;
}
printf("success\n");
return 0;
}Writing errors to stderr
By convention, normal output goes to stdout and errors go to stderr via fprintf(stderr, ...).
This lets users separate results from diagnostics when redirecting output.
#include <stdio.h>
int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Error: need an argument\n");
return 1;
}
printf("%s\n", argv[1]);
return 0;
}Quick Check
Test your understanding of CLI tool conventions.
Recap
You built a complete CLI tool:
- Define the interface, declare settings with defaults.
- Parse flags with
getopt; read positional args fromoptind. - Validate inputs before doing work.
- Return meaningful exit codes and send errors to
stderr.
Frequently asked questions
Is the “Building a CLI Tool” lesson free?
Yes — the full text of “Building a CLI Tool” is free to read here on the web, and the C Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO.
What will I learn in “Building a CLI Tool”?
Practical example. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C Academy?
No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Building a CLI Tool” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C Academy lesson?
Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- argc and argv
- Parsing Options
- getopt
- Building a CLI Tool