Hacker News new | past | comments | ask | show | jobs | submit login

I also use argv[0] for the -h help text, to show examples how to use the command.



There 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].


Nice, thank you.


wattttt? nice thank you


I'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.


Sometimes 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.


You don't need to. Keeping them shows the user exactly how to call the program based on how they called it.


Same here, as well as for showing an example invocation when the user fails to include a required argument.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: