0

I seem to remember reading that C language filter programs should interpret their first argument, if present, as the name of the input file. Is this correct?

I'm coding a filter that requires a parameter file, and I'd rather interpret the first argument as the name of that file. How should this situation be handled?

Steve
  • 1
  • 1
  • 2
    Not sure what exactly you mean by *"C language filter program"*, but if you mean a program that reads from stdin (or the file provided as the first argument) and outputs a filtered/modified version of the input, the canonical solution would be to take the parameter file as a (mandatory) *named argument* rather than a *positional argument*, e.g. `myFilter -params myParamFile myInputFile`. – Heinzi Sep 01 '22 at 08:04
  • Welcome to SO. You should provide some context about what type of filter programs you are talking. – Gerhardh Sep 01 '22 at 08:04
  • 1
    I don't think the language is at all relevant when it comes to command-line argument conventions. Your question lacks context. – Clifford Sep 01 '22 at 08:08
  • Hdinzi: thanks. Do you know of any written material that documents the conventions for writing a C filter? – Steve Sep 01 '22 at 08:09
  • 1
    What is a "C language filter program"? Do you mean "UNIX program often written in C"? Many programs accept 2-or-more input files to be processed. These are usually grouped together at the end of the command line... – Fe2O3 Sep 01 '22 at 08:09
  • 1
    Yes, it is standard to pass the name of an input file as a command-line argument. However, it is also standard to pipe the input to the program on `stdin` instead. If you want to do the former, you will have to use `argc` and `argv`. See the documentation of the function [`main`](https://en.cppreference.com/w/c/language/main_function) to learn the meaning of `argc` and `argv`. If you are having trouble understanding that documentation, then I recommend that you buy a [book](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) to learn C (beginner's level). – Andreas Wenzel Sep 01 '22 at 08:11
  • 2
    The programming language is irrelevant. There are no separate rules for C filters as opposed to C++ filters or Python filters. – n. m. could be an AI Sep 01 '22 at 08:15
  • You might want to switch the C tag to... POSIX tag maybe. Or whatever platform you are talking about. – hyde Sep 01 '22 at 08:16
  • 1
    You might solve this by putting the parameter file behind a cmand line switch: `mypipefilter -c config.conf` – hyde Sep 01 '22 at 08:18
  • Also, why do you care about this convention? Like, is it a requirement, or are you making a publicly available tool and want it to be "standard"? Just for your own satisfaction? – hyde Sep 01 '22 at 08:20
  • Look at the `sed` man page as an example... How does it load the edit script from a file? How is that specified? – Fe2O3 Sep 01 '22 at 08:21
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 01 '22 at 18:43
  • I don't think there are specific conventions for "C filter programs", but the [POSIX Utility Conventions](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html) and the [GNU Standards for Command Line Interfaces](https://www.gnu.org/prep/standards/html_node/Command_002dLine-Interfaces.html) might come close. – Heinzi Sep 05 '22 at 10:42

0 Answers0