#!perl

our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
our $DATE = '2021-07-15'; # DATE
our $DIST = 'App-quickabspath'; # DIST
our $VERSION = '0.001'; # VERSION

use strict;
use warnings;

use File::Spec;
use Getopt::Long qw(:config gnu_getopt no_ignore_case);

sub quickabspath {
    defined $ENV{PWD} && length($ENV{PWD})
        or die "quickabspath: PWD not defined or empty, we need it\n";
    File::Spec->rel2abs($_[0], $ENV{PWD});
}

sub parse_cmdline {
    my $res = GetOptions(
        'help|h' => sub {
            print <<USAGE;
Usage:
  quickabspath [OPTIONS]... <FILE>...
  quickabspath --version (-v)
  quickabspath --help (-h, -?)

Options:

For more details, see the manpage/documentation.
USAGE
            exit 0;
        },
        'version|v' => sub {
            no warnings 'once';
            print "quickabspath version ".($main::VERSION // 'dev').
                "\n";
            exit 0;
        },
    );
    if ($res) {
        if (!@ARGV) {
            warn "quickabspath: Please specify one or more files\n";
            $res = 0;
        }
    }
    exit 99 if !$res;
}

sub run {
    require Cwd;
    for my $path (@ARGV) {
        print quickabspath($path), "\n";
    }
}

# MAIN

parse_cmdline();
run();

1;
# ABSTRACT: Print the absolute path, without resolving symlinks
# PODNAME: quickabspath

__END__

=pod

=encoding UTF-8

=head1 NAME

quickabspath - Print the absolute path, without resolving symlinks

=head1 VERSION

This document describes version 0.001 of quickabspath (from Perl distribution App-quickabspath), released on 2021-07-15.

=head1 SYNOPSIS

 % quickabspath [OPTION]... <FILE>...

To demonstrate how C<realpath>, C<quickabspath>, and C<relpath> give you
different results:

 % pwd
 /home/ujang

 % mkdir dir1
 % ln -s dir1 sym1

 % cd sym1

 % realpath .      ;# gives absolute path and resolve symlinks
 /home/ujang/dir1
 % abspath .       ;# an alias for realpath
 /home/ujang/dir1

 % quickabspath .  ;# gives absolute path but does not resolve symlinks
 /home/ujang/sym1

 % relpath /home/ujang/dir1
 .
 % relpath /home/ujang/sym1
 ../sym1

=head1 DESCRIPTION

This is like the Unix utility B<realpath>, but without resolving symbolic links.
It requires C<PWD> environment variable from the shell. It then just uses
L<File::Spec>'s C<rel2abs> against the specified path. This means if the path is
already an absolute path, nothing will be done (no checking that path elements
exist, no resolving of symbolic links).

=head1 EXIT CODES

0 on success.

99 on command-line options error.

=head1 OPTIONS

=head1 FAQ

=head1 HOMEPAGE

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

=head1 SOURCE

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

=head1 BUGS

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

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

Unix utility B<realpath>

L<realpath>, L<relpath>

=head1 AUTHOR

perlancar <perlancar@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2021 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
