I have a program that expects arguments in the following syntax:
prog [-f filename | -g filename1 filename2] ...
Each filename must be prefixed with the
-f flag. For example, the following are valid invocations of
prog -f a.txt -g b.txt c.txt -f d.txt prog -g a.txt b.txt -g c.txt d.txt prog -f a.txt -f b.txt -f c.txt
…but the following are not:
prog -f a.txt b.txt prog -f a.txt -g b.txt prog a.txt
In my case, I only care about the
I have a lot of files in a directory, all of which end in
.txt. They look like this:
important-files/ ├── a.txt ├── b.txt ├── c.txt ├── d.txt └── filename with spaces.txt
I would like to avoid needing to list out every file one by one. Normally, I would use a straightforward glob for this:
$ prog important-files/*.txt
But this doesn’t work, since it produces the following invalid invocation:
$ prog important-files/a.txt important-files/b.txt important-files/c.txt important-files/d.txt 'important-files/filename with spaces.txt'
…when I really want this invocation:
$ prog -f important-files/a.txt -f important-files/b.txt -f important-files/c.txt -f important-files/d.txt -f 'important-files/filename with spaces.txt'
…since each filename must be prefixed with
-f in order for
prog to understand they shouldn’t be interpreted like
What is the shortest way to use a glob and prefix each of the files it expands to with a flag?