7 ms·
I also use argv[0] for the -h help text, to show examples how to use the command.
by dotancohen 2y ago
I also use argv[0] for the -h help text, to show examples how to use the command.
- anonymousiam 2y agoI've done this too, but you should remove the path elements from the argv[0] string before you include it in your error/help messages.
- jmholla 2y agoYou don't need to. Keeping them shows the user exactly how to call the program based on how they called it.
- Brian_K_White 2y agoSometimes you want it, sometimes you don't, so it needs to be in there, and sometimes you remove it yourself if your context of the moment doesn't want it. And neither the want-it nor the don't-want-it case is such an outlier that you can disregard and not serve that case. Sometimes you're talking to the user about general usage and the full path is a distracting detail and not the important part of the message. Sometimes the full path and truthful invoked filename are an unnecessary security disclosure like telling a web viewer details about the server. Sometimes the full path and truthful invoked filename is a necessary fact in debugging, or in errors, or even ordinary non-error logs that aren't public.
- st_goliath 2y agoThere is also a neat little BSD extension, also supported on a number of other Unix-like systems and GNU userspace (i.e. glibc, but also other libcs like Musl): extern char *__progname; which holds the program name without the (optional) invocation path in front of it. Basically the last path component of argv[0].
- dotancohen 2y agoNice, thank you.
- Brian_K_White 2y agowattttt? nice thank you
- patrickmay 2y agoSame here, as well as for showing an example invocation when the user fails to include a required argument.