Getopt::Long getting a string with spaces into a variable

后端 未结 4 1146
旧时难觅i
旧时难觅i 2021-01-15 09:42

I\'m making a perl script which uses Getopt::Long to parse command line arguments. However, I have an argument which can accept a string (with spaces). How can I get the who

4条回答
  •  滥情空心
    2021-01-15 10:33

    While I certainly agree that you should escape the arguments, if like me you have situation where you don't easily have control over the input then it is actually possible to do this with Getopt::Long.

    Example: [https://www.jdoodle.com/a/oHG]

    use strict;
    use Getopt::Long qw(GetOptions);
    
    
    my @spaces_string;
    
    GetOptions('test=s{1,}' => \@spaces_string);
    printf "String is: '%s'\n" ,join " ",@spaces_string;
    

    You are defining your option as having 1 or more values, assign it to an array and then join it back into a string.

    This is explained quite well under the 'Options with multiple values' section of the documentation: http://perldoc.perl.org/Getopt/Long.html#Options-with-multiple-values

提交回复
热议问题