R语言,Perl语言, Shell, Python传递参数的方法

本文探讨了在R、Perl、Shell和Python四种编程语言中如何解析命令行参数并输出帮助文档。通过实例展示了每种语言的具体实现方法,包括R的optparse包、Perl的Getopt::Long模块、Shell的getopts命令以及Python的argparse库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

包:

  • optparse
  • getopts
  • argparse

1, R语言

思路:
1, 加载optparse包
2, 设置参数, 为list
3, 解析参数
4, 如果参数为空, 打印帮助文档

#!/usr/bin/Rscript
library(optparse)

option_list <- list(
  make_option(c("-a", "--aa"), type = "integer", default=FALSE,
              help="Input a number"),
  make_option(c("-b", "--bb"), type="integer", default=FALSE,
              help="Input a number")
)

opt_parser = OptionParser(option_list=option_list);
opt = parse_args(opt_parser);
a = opt$a
b = opt$b
if (opt$a==0&&opt$b==0){
  print_help(opt_parser)
  stop("请输入参数", call.=TRUE)
  }
c = a+b
cat(" a is:",a,"\n","b is:",b,"\n","a plus b is:",c,"\n")

打印帮助文档:
帮助文档打印, 参数为空, 或者参数为-h或者–help时, 打印出帮助文档.

[dengfei@bogon qunti]$ Rscript plus.R
Usage: plus.R [options]


Options:
	-a AA, --aa=AA
		Input a number

	-b BB, --bb=BB
		Input a number

	-h, --help
		Show this help message and exit


Error: 请输入参数
Execution halted

正确输出结果:

[dengfei@bogon qunti]$ Rscript plus.R -a 1 -b 2
 a is: 1 
 b is: 2 
 a plus b is: 3 

2, Perl语言

参考: 在Perl、Shell和Python中传参与输出帮助文档https://zhuanlan.zhihu.com/p/53067406)

一个简单的示例, 一个perl程序, 计算a+b的值

有两个参数
-a
-b

代码如下:

#!/usr/bin/perl
use strict;
use Getopt::Long;

=head1 Description

    This script is calculate the sum of a and b

=head1 Usage

    $0 -a <input number> -b <input number> 

=head1 Parameters

    -a  [int]   Input raw number
    -b  [int]   Input raw number
=cut

my($a,$b);
GetOptions(
    "a:s"=>\$a,
    "b:s"=>\$b
    );

die `pod2text $0` if ((!$a) or (!$b));
print("The result of a plus b is:",$a+$b,"\n");

逻辑如下:
1, 使用perl 包 Getopt::Long进行参数书写
定义 a 和 a和 ab, 然后使用GetOptions函数
使用die函数, 如果没有相关参数, 就die

my($a,$b);
GetOptions(
    "a:s"=>\$a,
    "b:s"=>\$b
    );

die `pod2text $0` if ((!$a) or (!$b));

2, 使用POD文档, 编写指南
包括:
描述
使用方法
参数介绍

=head1 Description

    This script is calculate the sum of a and b

=head1 Usage

    $0 -a <input number> -b <input number> 

=head1 Parameters

    -a  [int]   Input raw number
    -b  [int]   Input raw number
=cut

**打印帮助文档: **
如果没有添加参数:

[dengfei@bogon qunti]$ perl 1.pl 
Description
        This script is calculate the sum of a and b

Usage
        $0 -a <input number> -b <input number>

Parameters
        -a  [int]   Input raw number
        -b  [int]   Input raw number

正确输出结果:

[dengfei@bogon qunti]$ perl 1.pl -a 1 -b 2
The result of a plus b is:3

3, Shell脚本传递参数

思路:
1, 定义一个名为helpdoc的函数, 如果没有参数, 就返回这个函数, 打印出帮助信息
2, 定义参数, 使用getopts, 参数放在顿号里面, 可以将参数赋值给a和b, 下面直接调用 a 和 a和 ab即可
3, 执行加号, 打印结果.

#!/bin/bash

# 定义帮助文档
helpdoc(){
    cat <<EOF
Description:
    This is the description.

Usage:

    $0 -a <input number> -b <input number> 

Option:

    -a    a number
    -b    b number
EOF
}

# 如果没有参数, 输出帮助文档
if [ $# = 0 ]
then
    helpdoc
    exit 1
fi

while getopts ":a:b:" opt
do
    case $opt in
        a)
            a=$OPTARG
            ;;
        b)
            b=$OPTARG
            ;;
        ?)
            echo "Unknown option: $opt"
            helpdoc
            exit 1
            ;;
    esac
done
sum=$(expr $b + $a)    
echo "a is $a"
echo "b is $b"
echo "a plus b is:$sum"

**打印帮助文档: **

[dengfei@bogon qunti]$ bash plus.sh
Description:
    This is the description.

Usage:

    plus.sh -a <input number> -b <input number> 

Option:

    -a    a number
    -b    b number

正确输出结果:
[dengfei@bogon qunti]$ bash plus.sh -a 1 -b 2

a is 1
b is 2
a plus b is:3

4. Python脚本传递参数

思路:

  • 载入argparse
  • 增加-a-b两个参数
  • 赋值给opt
  • 通过opt.aopt.b使用参数
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("-a", help="first number",type=int)
parser.add_argument("-b", help="second number",type=int)

opt = parser.parse_args()

a1 = opt.a
b1 = opt.b

c = a1 + b1

print(a1," plus ",b1," is ",c,"\n")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值