#!perl

our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
our $DATE = '2020-08-26'; # DATE
our $DIST = 'App-ArgUtils'; # DIST
our $VERSION = '0.001'; # VERSION

use strict;
use warnings;
use Getopt::Long;

my $opt_null = 0;
my $opt_quote = 0;

Getopt::Long::Configure(qw/gnu_getopt no_ignore_case/);
GetOptions(
    '0' => \$opt_null,
    'q' =>  \$opt_quote,
) or die "first-arg: Error in parsing options, aborting\n";

for (@ARGV ? ($ARGV[0]) : ()) {
    if ($opt_quote) {
        require String::ShellQuote;
        print String::ShellQuote::shell_quote($_);
    } else {
        print;
    }
    print $opt_null ? "\0" : "\n";
}

# ABSTRACT: Print the first command-line argument to standard output
# PODNAME: first-arg

__END__

=pod

=encoding UTF-8

=head1 NAME

first-arg - Print the first command-line argument to standard output

=head1 VERSION

This document describes version 0.001 of first-arg (from Perl distribution App-ArgUtils), released on 2020-08-26.

=head1 SYNOPSIS

Usage:

 % first-arg [option] [arg]...

Examples:

 % first-arg "first arg" second third
 first arg

 # but usually useless to use with backtick
 % media-info `first-arg *`         ; # problem with quoting
 % media-info `first-arg -q *`      ; # problem with quoting
 % ls | head -n1 | xargs media-info ; # problem with quoting

 # use with shell's read
 % first-arg * | ( read p && media-info "$p" )

=head1 DESCRIPTION

=head1 OPTIONS

=head2 -q

Shell-quote the argument.

=head2 -0

Print null character ("\0") after the argument, instead of newline ("\n").

=head1 HOMEPAGE

Please visit the project's homepage at L<https://metacpan.org/release/App-ArgUtils>.

=head1 SOURCE

Source repository is at L<https://github.com/perlancar/perl-App-ArgUtils>.

=head1 BUGS

Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=App-ArgUtils>

When submitting a bug or request, please include a test-file or a
patch to an existing test-file that illustrates the bug or desired
feature.

=head1 SEE ALSO

L<last-arg>

=head1 AUTHOR

perlancar <perlancar@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2020 by perlancar@cpan.org.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut
