5

In the manpage of xclip

-selection

specify which X selection to use, options are 
"primary" to use XA_PRIMARY (default), 
"secondary" for XA_SECONDARY or 
"clipboard" for XA_CLIPBOARD

Note that only the first character of the selection specified with the -selection option is important. 
This means that "p", "sec" and  "clip"  would
have the same effect as using "primary", "secondary" or "clipboard" respectively.

The following for using clipboard selection works

    xclip -sel clip < ~/.ssh/id_rsa.pub

the manpage says that clipboard can be shortened to clip, but doesn't say that -selection can be shortened to -sel.

So why does it work? Does this feature for specifying an option belong to xclip, or also to many other applications besides xclip?

Tim
  • 101,790
  • Do you mean to ask a specific question about xclip/paste and a general option-shortening one in the same Q? The second seems independent -- and broad. – Jeff Schaller Jun 11 '16 at 19:54

2 Answers2

3

xclip uses the X Toolkit library, which does the options-parsing. All of the options can be abbreviated. The library only gives an error if there is an ambiguity.

Options, of course, are things like -select which can be abbreviated as -sel (possibly even -s).

xterm uses the same library, same behavior. It uses special cases (outside the library) to make the -v command a unique abbreviation for -version, etc.

X Toolkit uses a single dash - for options, and does not distinguish between "short" and "long" because it is not an extension of getopt. As I pointed out in Single dashes - for single-character options, but double dashes -- for words?, it was introduced around the same time as GNU getopt, which did extend getopt. This was before POSIX came along, but AT&T getopt had several years of use, establishing its role for single-character options. GNU getopt uses a double-dash -- to denote long options.

Noting a long digression, you can read the source code for GNU getopt (which is unrelated to xclip) from its git repository, e.g.,

 369    Long-named options begin with `--' instead of `-'.
 370    Their names may be abbreviated as long as the abbreviation is unique
 371    or is an exact match for some defined option.  If they have an
 372    argument, it follows the option name in the same ARGV-element, separated
 373    from the option name by a `=', or else the in next ARGV-element.
 374    When `getopt' finds a long-named option, it returns 0 if that option's
 375    `flag' field is nonzero, the value of the option's `val' field
 376    if the `flag' field is zero.
Thomas Dickey
  • 76,765
  • thanks. does getopt library also allow shortening options in the same way as the X toolkit library? – Tim Jun 11 '16 at 22:16
  • 1
    getopt is a standard function that only knows about single-character options. GNU getopt_long permits abbreviations of long options. – Thomas Dickey Jun 11 '16 at 22:33
  • getopt_long only support single character abbreviation, does it? – Tim Jun 11 '16 at 22:41
  • getopt_long (like X Toolkit) allows any length. But if two options match with a given length, that would produce a warning about ambiguous options. – Thomas Dickey Jun 11 '16 at 23:24
  • The link you gave says "To accept GNU-style long options as well as single-character options, use getopt_long instead of getopt." For example, it doesn't say that option -select can be shorten to be -sel but only to -s – Tim Jun 12 '16 at 14:15
  • I got that by running commands to test them. You can also read the source code. The documentation needs some work. – Thomas Dickey Jun 12 '16 at 14:16
  • Is parsing arbitrary shortened option implemented in terms of regex matching? – Tim Jun 12 '16 at 14:17
  • Is it correct that -sel by shortening -select is still a long option, not a short option, even though -sel starts with - not --? – Tim Jun 12 '16 at 14:35
  • Option -s doesn't throw an error, but on my system, it seems to always get the contents of the primary selection, regardless of what argument you give it, even no argument at all. I dug through the documentation and even poked around the source code but couldn't find any info about it. Is this a bug? – wjandrea Nov 02 '17 at 22:46
1

More recent manpages (at least) say:

Options can be abbreviated as long as they remain unambiguous. For example, it is possible to use -d or -disp instead of -display. However, -v couldn't be used because it is ambiguous (it could be short for -verbose or -version), so it would be interpreted as a filename.

That means that -se is sufficient for -selection but is not as good, mnemonically as -sel.

Walf
  • 1,321