Re: [MacPerl-Porters] Porting from OSX to OS9

[email protected] (Axel Rose) Thu, 17 Oct 2002 14:40:01 +0200
Newsgroups perl.macperl.porters
Message-ID <p04330104b9d45fb1c6f1@[172.16.172.241]>
Hello Oorbee,

I copied some samples from my local Perl scripts. They
show some options how you could make entering options
under MacPerl a bit more user-friendly. There are much
more elaborate ways possible, but ... 


HTH


Axel


# enter a file path
chomp( my $pwd = `pwd` );
my $file = $ARGV[0] || MacPerl::Ask( "input:", $pwd );


# ask for choices
my $choice = MacPerl::Answer( "Options:", "-A-", "-B-" );


# simulate Unix like command line
my $MACOS = ( $^O =~ /Mac/ ) || 0;
if ($MACOS) {
    my $ans =
      MacPerl::Ask( 'Please enter @ARGV (-h for help)',
        defined $ARGV[0] ? $ARGV[0] : "" );
    if ( $ans =~ /\b-h\b/i ) { @ARGV = ('-h') }
    else {
        my $args = main::splitargs($ans);
        @ARGV = @$args;
    }
}

sub splitargs {
    my @return;

    my $s = shift;
    $s =~ s/^\s*//;
    $s =~ s/\s*$//;

    my @args = split //, $s;
    my $inparam   = 0;
    my $fileparam = 0;
    my $param     = 0;
    my $pos       = 0;
    for (@args) {
        if ( /^-/ and !$inparam and !$fileparam ) {
            $return[$param] = $_;
            $inparam   = 1;
            $fileparam = 0;
            $pos++;
        }
        elsif ( $inparam and !$fileparam ) {
            $return[$param] .= $_;
            if ( $args[ $pos + 1 ] and $args[ $pos + 1 ] eq ' ' ) {
                $inparam   = 0;
                $fileparam = 1;
                $param++;
            }
            $pos++;
        }
        elsif ( !$inparam and $fileparam ) {
            $return[$param] .= $_;
        }
        elsif ( !$inparam and !$fileparam ) {
            $return[$param] .= $_;
            $fileparam = 1;
        }
        else {
            warn "internal logic error\n";
        }
    }
    $return[-1] =~ s/^\s+//;
    return \@return;
}