nategood / commando

An Elegant CLI Library for PHP
MIT License
798 stars 80 forks source link

Impossible to read filename or '-' #91

Open OnkelTem opened 6 years ago

OnkelTem commented 6 years ago

It's very common for a file processing script to either take the filename OR '-' to get data from stdin. With this library I cannot do this:

php convert.php input.log
php convert.php - < input.log

Whenever I pass "-" it says

ERROR: Unable to parse option -: Invalid syntax

My definition was like:

  $cmd->option()
    ->require()
    ->describedAs('Input log file or `-` to read from stdin');

and value reading part was as simple as:

  $filename = $cmd[0];
NeoVance commented 6 years ago

At the moment our parser can not handle - without a short option following it. The majority of bash commands handle stdin input without the need for the dash at the end of the command so it has not been a priority to implement.

In any case; reading data from stdin is beyond the scope of this library, being a command line argument parser, and is left for the user to implement.

You can use needs and/or map to handle arguments that are required to be supplied or optionally piped in through stdin.

OnkelTem commented 6 years ago

The majority of bash commands handle stdin input without the need for the dash at the end of the command...

I'm not sure about what do you exactly mean under 'bash commands', but that's of course not true for the majority of standard set of *nix programs (e.g.: tar). Any detection magic is more a flaw than a feature. Programs shouldn't make any assumptions about their environment unless they're told explicitly what to do. And detecting stdin/stdout on PHP particularly - is a well know kind of perversion, yeah :)

You can use needs and/or map to handle arguments that are required to be supplied or optionally piped in through stdin.

Thanks, I'll take a look. Another workaround I used - I tweaked $_SERVER['argv}'] taking my positional argument out of there.

NeoVance commented 6 years ago

@OnkelTem Initializing Command($tokens) with a modified token list can be useful! That is usually how I do my sub-commands.