Cobra: Support for I/O redirection

Created on 1 Apr 2016  路  4Comments  路  Source: spf13/cobra

How can a cobra command receive output of another command as arguments.
for example if my command is print [items...]

I like to redirect output of file containing items to print command.
cat items | print

I can manually read piped args using os.stdin though.

kinstale

Most helpful comment

xargs is the way to go, no need to fiddle around with os.Stdin or such.

cat items | xargs print

man xargs for more info ;)

All 4 comments

os.stdin is how I would think you would do it. Is there an alternative solution you were thinking of?

I was thinking if the redirected output line from other command can be passed in as args in func(cmd *cobra.Command, args []string) error. or Cobra can implicitly execute the Cmd per each argument piped from another command by passing the arg in args []string.

os.Stdin is what I'd expect to use; piping commands together is supposed to connect the stdout/stdin of the commands not apply them as arguments. By putting them as arguments you'd have to know how to split the output into args too and deal with unmatched quotes, spaces, etc.

xargs is the way to go, no need to fiddle around with os.Stdin or such.

cat items | xargs print

man xargs for more info ;)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

umbynos picture umbynos  路  5Comments

diwakergupta picture diwakergupta  路  5Comments

lukasmalkmus picture lukasmalkmus  路  4Comments

phanirithvij picture phanirithvij  路  5Comments

galileo-pkm picture galileo-pkm  路  6Comments