How to enforce docopt to parse one option only? - docopt

folks!
I am trying the docopt (cpp variant). I have tried this variant:
Usage:
prog [-o | --out-file=<out-file>] <in-file>
prog -h | --help
prog --version
Options:
-h --help Show this screen.
--version Show version.
-o, --out-file=<out-file> Output file name [default: stdout].
<in-file> Input file.
I expected that docopt expects zero or one out-file option and it gives me a string as a result, but it can accept two or more this options and gives me a string-list value.
Is this right?

I found that it works as expected when I corrected the command-line description like this:
Usage:
prog [-o<out-file>|--out-file=<out-file>] <in-file>
prog -h | --help
prog --version
Options:
-h --help Show this screen.
--version Show version.
-o, --out-file=<out-file> Output file name [default: stdout].
<in-file> Input file.
or even like this:
Usage:
prog [-o<out-file>] <in-file>
prog -h | --help
prog --version
Options:
-h --help Show this screen.
--version Show version.
-o, --out-file=<out-file> Output file name [default: stdout].
<in-file> Input file.

Related

Why is fish redirection not working as expected

When I try to redirect fish shell output to a file, the command gets put in the file, not the command output. why? This works as expected in bash, zsh etc.
example:
$ fish --version
fish, version 3.2.0
$ rm -f /tmp/foo
$ echo bar > /tmp/foo
$ cat /tmp/foo
echo bar
# expected output:
bar
# It's not related to the echo command. e.g:
$ ls -l > /tmp/foo
$ cat /tmp/foo
ls -l
This seems to have been caused by a bug in fishgretel/fasd
See
https://github.com/fishgretel/fasd/commit/98fb3873aae9adcca2ffc4b4b3958e45d74cb894

lxc option "--" when calling lxc-start / lxc-create

What is the significance of -- in the command line of commands like lxc-create or lxc-start.
I tried to use Google in order to get an answer but without success.
// Example 1
lxc-create -t download -n u1 -- -d ubuntu -r DISTRO-SHORT-CODENAME -a amd64
// Example 1
application="/root/app.out"
start="/root/lxc-app/lxc-start"
$start -n LXC_app -d -f /etc/lxc/lxc-app/lxc-app.conf -- $application &
As explained in the references provided in the comments, the "--" indicates the end of the options passed to the command. The following parameters/options will be eventually used by a sub-command called by the command.
In your example:
lxc-create -t download -n u1 -- -d ubuntu -r DISTRO-SHORT-CODENAME -a amd64
lxc-create command will interpret "-t download -n u1" and the remaining "-d ubuntu -r DISTRO-SHORT-CODENAME -a amd64" will be passed to the template script which will configure/populate the container.
In this specific example, the "-t download" makes lxc-create run a template script named something like "/usr/share/lxc/templates/lxc-download" to which it will pass "-d ubuntu -r DISTRO-SHORT-CODENAME -a amd64".

How do I get pipe to work when passed in string for sh -c and su is involved

I have the below command
su - root -s /usr/bin/sh -c "java -version|grep build"
But the pipe to grep doesn't seem to work since it prints all lines regardless
java -version writes to standard error, not standard output.
su - root -s /usr/bin/sh -c "java -version 2>&1 |grep build"
2>&1 copies standard error to standard output, so that it gets fed through the pipe and into grep.
That said, grep doesn't have to be run as root, assuming the pipeline isn't being specified for use by something that just runs arbitrary code using su sh -c '...'.
su - root -s /usr/bin/sh -c 'java -version' 2>&1 | grep build

Redirecting of jq output

In a terminal this works fine:
mosquitto_sub -h 192.168.178.20 -t tele/POW/SENSOR/# | jq '.ENERGY|.Power'
Every 10 seconds there is an output on screen because the device POW publishes it's sensor dates every 10 seconds. The output of mosquitto_sub (it's a JSON string) is piped to jq and jq shows only the value digit of the key 'Power'. Now I try to store the jq output (only the value) to a file 'output.log'.
mosquitto_sub -h 192.168.178.20 -t tele/POW/SENSOR/# | jq '.ENERGY|.Power' > output.log
is not working. What is going wrong?
From the jq manual:
--unbuffered
Flush the output after each JSON object is printed
(useful if you’re piping a slow data source into
jq and piping jq’s output elsewhere).

Why does pg_restore return "options -d/--dbname and -f/--file cannot be used together"?

The following Windows batch script fails on the line with database restore:
#Echo off
set Path=C:\Program Files (x86)
set Backup_Path=C:\Program Files (x86)
c:
cd \
cd C:\Program Files (x86)\PostgreSQL\9.1\bin
#echo "Wait ..."
setlocal
set PGPASSWORD=1234
psql.exe -U postgres -c "create database Mydata"
"C:\Program Files (x86)\PostgreSQL\9.1\bin\pg_restore.exe" -h localhost -U postgres -d Mydata -f "Mydata.backup"
pause
endlocal
The error is:
pg_restore : -d / - dbname and -f / - file can not be used together
Try " pg_restore --help "
-f is the output filename, not the input file.
The input file does not have any parameter switch.
c:\>pg_restore --help
pg_restore restores a PostgreSQL database from an archive created by pg_dump.
Usage:
pg_restore [OPTION]... [FILE]
General options:
-d, --dbname=NAME connect to database name
-f, --file=FILENAME output file name
-F, --format=c|d|t backup file format (should be automatic)
-l, --list print summarized TOC of the archive
-v, --verbose verbose mode
-V, --version output version information, then exit
-?, --help show this help, then exit
So you need to use:
pg_restore.exe -h localhost -U postgres -d Mydata "Mydata.backup"
More details in the manual: http://www.postgresql.org/docs/current/static/app-pgrestore.html