Skip to content

Commit

Permalink
Rename --batch-stdin to --batch
Browse files Browse the repository at this point in the history
... and pass '-' as an argument
  • Loading branch information
perlpunk committed Jan 18, 2024
1 parent ddf847d commit d1f84b3
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 22 deletions.
11 changes: 8 additions & 3 deletions etc/spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,14 @@ description: |
=item Process a list of files from stdin
# Tidy all .yaml files that are in git
% git ls-files | grep '.yaml$' | yamltidy --batch-stdin --inplace
% git ls-files | grep '.yaml$' | yamltidy --inplace --batch -
# short options
% git ls-files | grep '.yaml$' | yamltidy -i -b -
# Only tidy modified files
% git ls-files --modified | grep '.yaml$' | yamltidy --batch-stdin --inplace
% git ls-files --modified | grep '.yaml$' | yamltidy --inplace --batch -
In the future yamltidy can take a directory as an argument and process
file name patterns from configuration.
=item Use a certain configuration file
Expand Down Expand Up @@ -92,7 +97,7 @@ options:
- debug --Debugging output
- partial --Input is only a part of a YAML file
- indent=i --Override indentation spaces from config
- batch-stdin --Tidy all file names passed via STDIN
- batch|b --Tidy all files - currently requires parameter "-" for filenames passed via STDIN
- verbose --Output information
- help|h --print usage message and exit
- version --Print version information
Expand Down
10 changes: 7 additions & 3 deletions lib/YAML/Tidy/Run.pm
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ my @options = (
[ 'debug' => 'Debugging output' ],
[ 'partial' => 'Input is only a part of a YAML file' ],
[ 'indent=i' => 'Override indentation spaces from config' ],
[ 'batch-stdin' => 'Tidy all file names passed via STDIN' ],
[ 'batch|b' => 'Tidy all files - currently requires parameter "-" for filenames passed via STDIN' ],
[ 'verbose|v' => 'Output information' ],
[],
[ 'help|h', "print usage message and exit", { shortcircuit => 1 } ],
Expand Down Expand Up @@ -70,9 +70,13 @@ EOM
return;
}

if ($opt->batch_stdin) {
if ($opt->batch) {
my ($path) = @ARGV;
unless ($path eq '-') {
die "--batch currently requires '-' to receive filenames via STDIN\n";
}
unless ($opt->inplace) {
die "--batch-stdin currently requires --inplace";
die "--batch currently requires --inplace\n";
}
my $in = $self->{stdin};
while (my $file = <$in>) {
Expand Down
29 changes: 17 additions & 12 deletions lib/yamltidy.pod
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,14 @@ directly.
=item Process a list of files from stdin

# Tidy all .yaml files that are in git
% git ls-files | grep '.yaml$' | yamltidy --batch-stdin --inplace
% git ls-files | grep '.yaml$' | yamltidy --inplace --batch -
# short options
% git ls-files | grep '.yaml$' | yamltidy -i -b -
# Only tidy modified files
% git ls-files --modified | grep '.yaml$' | yamltidy --batch-stdin --inplace
% git ls-files --modified | grep '.yaml$' | yamltidy --inplace --batch -

In the future yamltidy can take a directory as an argument and process
file name patterns from configuration.

=item Use a certain configuration file

Expand Down Expand Up @@ -92,16 +97,16 @@ Vim example configuration:

=head2 GLOBAL OPTIONS

--config-file -c Config file
--config-data -d Configuration as a string
--inplace -i Edit file inplace (flag)
--debug Debugging output (flag)
--partial Input is only a part of a YAML file (flag)
--indent Override indentation spaces from config
--batch-stdin Tidy all file names passed via STDIN (flag)
--verbose Output information (flag)
--help -h print usage message and exit (flag)
--version Print version information (flag)
--config-file -c Config file
--config-data -d Configuration as a string
--inplace -i Edit file inplace (flag)
--debug Debugging output (flag)
--partial Input is only a part of a YAML file (flag)
--indent Override indentation spaces from config
--batch -b Tidy all files - currently requires parameter "-" for filenames passed via STDIN (flag)
--verbose Output information (flag)
--help -h print usage message and exit (flag)
--version Print version information (flag)


=head2 SUBCOMMANDS
Expand Down
8 changes: 4 additions & 4 deletions t/02.run.t
Original file line number Diff line number Diff line change
Expand Up @@ -100,24 +100,24 @@ subtest file => sub {
clean();
};

subtest 'batch-stdin' => sub {
subtest 'batch stdin' => sub {
my @f;
local *{"YAML::Tidy::Run::_process_file"} = sub($, $file) { push @f, $file };
open my $in, '<', \$filelist;
local @ARGV = (qw/ --batch-stdin --inplace /);
local @ARGV = (qw/ -b - --inplace /);
$ytr = YAML::Tidy::Run->new(stdin => $in);
$ytr->run;
is $f[0], 'a/b/1.yaml', 'file 1';
is $f[1], 'c/d/2.yaml', 'file 2';
clean();

local @ARGV = (qw/ --batch-stdin /);
local @ARGV = (qw/ --batch - /);
$ytr = YAML::Tidy::Run->new(stdin => $in);
eval {
$ytr->run;
};
my $err = $@;
like $err, qr/--batch-stdin currently requires --inplace/, '--inplace required';
like $err, qr/--batch currently requires --inplace/, '--inplace required';
clean();
};

Expand Down

0 comments on commit d1f84b3

Please sign in to comment.