r/C_Programming • u/Existing-Pudding6537 • Aug 03 '26
getopt_long Design: How to clean up flag naming & mutual exclusion in C?
Hi everyone,
I'm building veilbit, a simple command-line steganography tool, to learn C. Right now, I'm working on how the program reads commands using getopt_long.
I want to avoid using -h for "hide" because -h is usually saved for --help. I have three ways to design the command interface:
Option 1: Implicit Mode – The program decides what to do based on whether you use the message flag (-m or -f).
bash
vb -i input.png -o output.png -m "secret" # Hide
vb -i input.png # ExtractOption 2: Explicit -c / -x – Like tar, using "create" and "extract".
bash
vb -c -i input.png -o output.png -m "secret"
vb -x -i input.pngOption 3: Explicit -e / -x – Using "embed" and "extract".
bash
vb -e -i input.png -o output.png -m "secret"
vb -x -i input.pngMy questions:
- User experience: Is Option 1 (implicit) good and easy to use? Or do most developers prefer explicit flags like in Options 2 and 3?
- Naming: Which pair is clearer –
-c/-xor-e/-x? - Implementation: How do you make sure the user can only pick one mode (embed or extract) when using
getopt_long?
Repo link for context: https://github.com/tkalum/veilbit
Thanks!
•
u/github-guard Aug 03 '26
🔍 GitHub Guard: Trust Report
This project scored 3/6 on our safety audit.
Audit Breakdown:
6
u/thank_burdell Aug 03 '26
The way I handle it is: parse the passed-in options, set flags accordingly, then a separate step to check the flags for logical inconsistencies or invalid combinations. Exit with a usage() printout if anything’s amiss, otherwise begin execution.
2
u/skeeto Aug 03 '26
Neat project! This doesn't engage with your question, but watch your sign bits!
bitmust be unsigned at least for the shift. While setting that up I noticed 1-bit-per-pixel images aren't handled properly:While looking into that I also found UB in libpng:
(libpng is a real dumpster fire of a library. Each time I interact with it, it never fails to shock me how garbage it is.)
1
u/AutoModerator Aug 03 '26
Hi /u/Existing-Pudding6537,
Your submission in r/C_Programming was filtered because it links to a git project.
You must edit the submission or respond to this comment with an explanation about how AI was involved in the creation of your project.
While AI-generated code is not disallowed, low-effort "slop" projects may be removed and it's likely that other users push back strongly on substantially AI-generated projects.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/SmokeMuch7356 Aug 04 '26
-e(embed) makes more sense to me, but either will work.You'll have to keep track of which options have been selected and enforce exclusivity manually:
1
u/Anode1_dev Aug 04 '26
There is no one design that fits every tool. getopt_long is flexible enough to adjust to the application, and different applications reasonably choose different styles: compare tar, git and find. I followed the conventions in Robbins' book at first, then found that what is convenient depends on what you type most often.
For your case: -e/-x, since tar's -c means "create an archive" and that is not what you are doing. Enforce it with a mode variable that refuses the second flag, and after the loop treat "still unset" as a usage error. Forgetting -m should be an error, not a silently different operation.
In one of mine I even mix bare arguments in, purely because it is the common case and less typing: https://github.com/Anode1/ais/blob/main/doc/command_line.txt
0
u/pjl1967 Aug 04 '26 edited Aug 04 '26
Option 2.
As for your question 3, just write a function to check afterwards, e.g. here.