bug-datamash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: "CHAR_MIN - X" in help/version flags


From: Shawn Wagner
Subject: Re: "CHAR_MIN - X" in help/version flags
Date: Fri, 5 Aug 2022 19:29:59 -0700

> Yet, when I attempt to do something like `datamash -~` or `datamash -}`, it returns an invalid option error.

They're not listed in the short options passed to getopt_long(), hence the error. But ~ isn't actually what CHAR_MIN - 2 is; it's just what you get when you convert an int outside of the range of chars to a char. Those values are ints that can't be represented in a char type, meaning they can't go in the short options so they're ideal for a long-only option to avoid any chance of future conflicts - only so many single character options available to use. If you want to make -h a synonym for --help, you'd have to add it to the short options and change the --help entry in the long options to return 'h', and adjust the argument-handling switch too.

On Fri, Aug 5, 2022 at 3:25 PM Tim Rice <trice@posteo.net> wrote:
Hey all,

I was thinking GNU Datamash should accept -h and -V for printing help and version respectively. It turns out the relevant code is in system.h, where we can see:

```
/* These enum values cannot possibly conflict with the option values
    ordinarily used by commands, including CHAR_MAX + 1, etc.  Avoid
    CHAR_MIN - 1, as it may equal -1, the getopt end-of-options value.  */
enum
{
   GETOPT_HELP_CHAR = (CHAR_MIN - 2),
   GETOPT_VERSION_CHAR = (CHAR_MIN - 3)
};

#define GETOPT_HELP_OPTION_DECL \
   "help", no_argument, NULL, GETOPT_HELP_CHAR
#define GETOPT_VERSION_OPTION_DECL \
   "version", no_argument, NULL, GETOPT_VERSION_CHAR
```

When I do a printf on those GETOPT_(HELP|VERSION)_CHAR, I get '~' and '}'.

Yet, when I attempt to do something like `datamash -~` or `datamash -}`, it returns an invalid option error. I got the same response with various combinations of quotes and backslash-escapes around those characters.

It seems like similar code is in other GNU software like coreutils, sed and tar, which makes me wonder if there is some good reason why GNU has adopted the convention. On the other hand, I notice GNU Awk simply allows 'h' and 'V'. The relevant code is around line 200 of main.c in the gawk sources.

This provokes some questions:

* Is there an additional way of escaping our current options which will make them work correctly?

* What might be the reason for using these unconventional symbols?

* Does anyone see a problem with changing it to be like GNU Awk?

Searching the web has not provided any useful information.

Kind regards,

Tim


reply via email to

[Prev in Thread] Current Thread [Next in Thread]