in reply to Re: Closed:how to argparse same option to array instead of overwrite
in thread Closed:how to argparse same option to array instead of overwrite

humbly ask, how? since It assign args to different scalar, I want keep order globally. can you give me an example code then?
  • Comment on Re^2: Closed:how to argparse same option to array instead of overwrite

Replies are listed 'Best First'.
Re^3: Closed:how to argparse same option to array instead of overwrite
by tybalt89 (Monsignor) on Jul 10, 2024 at 01:13 UTC
    #!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11160457 use warnings; use Getopt::Long; $SIG{__WARN__} = sub { die @_ }; @ARGV = qw( --foo 123 --bar 456 --foo 789 something extra ); use Data::Dump 'dd'; dd '@ARGV before', \@ARGV; my @fooarray; my @bararray; GetOptions 'foo=s' => \@fooarray, 'bar=s' => \@bararray; use Data::Dump 'dd'; dd 'fooarray', \@fooarray; use Data::Dump 'dd'; dd 'bararray', \@bararray; use Data::Dump 'dd'; dd '@ARGV after', \@ARGV;

    Outputs:

    ( "\@ARGV before", ["--foo", 123, "--bar", 456, "--foo", 789, "something", "extra"], ) ("fooarray", [123, 789]) ("bararray", [456]) ("\@ARGV after", ["something", "extra"])
      yeah, thanks for your code.But I want outputs to be
      ("fooarray", [123])("bararray",[456])("fooarray",[789])
      ,Must keep order, there is a chained transform based on it, so I write my argparser.It works fine.
        Maybe this example is what you're looking for?
        my @script; GetOptions( ... 'call=s' => sub { push @script, [ call_method => $_[1] ] }, 'eval=s' => sub { push @script, [ do_eval => $_[1] ] }, 'out|o=s' => sub { push @script, [ output => $_[1] ] }, '<>' => sub { push @script, [ process_tpl => $_[0] ] }, ) or pod2usage(2); ... sub call_method { ... } sub do_eval { ... } sub output { ... } sub process_tpl { ... } # All the global options are taken care of. Now execute the "script o +ptions" # in the order they were given. for (@script) { my ($method, @args)= @$_; $method= main->can($method) or die 'bug'; $method->(@args); }
Re^3: Closed:how to argparse same option to array instead of overwrite
by ikegami (Patriarch) on Jul 10, 2024 at 04:13 UTC

    Ah, I misread. You are completely misusing options. You should provide a better interface.

      English is not my native language, and my code is not buggy, it works just like normal argparser do and deal with filenames start with '-', the logic is subtle.

        I spotted at least one serious bug, and one missing important feature. But the comment to which you replied makes no mention of that??? It says the interface you are trying to provide is poor.

      So you hate ffmpeg then?