标签: arguments

如何从 Perl 将参数传递给外部进程?

我有一个应用程序可执行文件,它使用不同的参数运行以产生不同的输出。我想从脚本的命令行参数中为此提供一些参数,而其他参数则是脚本的本地参数。用法:

./dump-output.pl <version> <folder-name> <output-file>


my $version = $ARGV[0];
my $foldername = $ARGV[1];
my $outputfile = $ARGV[2];
my $mkdir_cmd = "mkdir -p ~/$foldername";

# There are 6 types of outputs, which can be created:
# 'a', 'b', 'c', 'd', 'e' or 'f'
my @outputtype = ('a', 'b', 'c', 'd', 'e', 'f');

my $mkdir_out = `$mkdir_cmd`;

for( $itr=0; itr<=5; itr++ ) {
    $my_cmd = "./my_app -v $version -t $outputtype[itr] -f $outputfile > ~/$foldername/$outputtype.out"
    $my_out = `$my_cmd`;
}
Run Code Online (Sandbox Code Playgroud)

我对上面的代码做了一些固有的错误,但一直无法弄清楚:-(

perl command-line arguments external-process

0
推荐指数
1
解决办法
2008
查看次数

宏作为函数参数的默认参数

我正在写记录器文件。我更喜欢在那里使用宏__FUNCTION__。我不喜欢这样的方式:

Logger.write("Message", __FUNCTION__);
Run Code Online (Sandbox Code Playgroud)

也许,有可能做这样的事情:

void write(const string &message, string functionName = __FUNCTION__)
{
   // ...
}
Run Code Online (Sandbox Code Playgroud)

如果没有,有没有什么方法可以不用手来做这些事情(我的意思是传递函数名)?

c++ macros arguments default function

0
推荐指数
1
解决办法
1484
查看次数

如何将当前目录作为参数传递给脚本?

这在 Python 中可能吗?我在 linux 上工作,所以我想知道是否有办法在调用脚本时组合 bash 命令(类似于 pwd)。

python arguments

0
推荐指数
1
解决办法
4372
查看次数

如何从父解析器获取 argparse 子解析器(检查默认值)

假设我创建了一个带有参数默认值的解析器,然后给它一个带有参数默认值的子解析器。

In [1]: parser = argparse.ArgumentParser(description='test')

In [2]: parser.add_argument("--test", dest="test", default="hello")
Out[2]: _StoreAction(option_strings=['--test'], dest='test', nargs=None, const=None, default='hello', type=None, choices=None, help=None, metavar=None)

In [3]: parser.get_default("test")
Out[3]: 'hello'

In [4]: subparsers = parser.add_subparsers(dest="command")

In [5]: parser_other = subparsers.add_parser("other")

In [6]: parser_other.add_argument("--other-test", dest="other_test", default="world")
Out[6]: _StoreAction(option_strings=['--other-test'], dest='other_test', nargs=None, const=None, default='world', type=None, choices=None, help=None, metavar=None)

In [7]: parser_other.get_default("other_test")
Out[7]: 'world'
Run Code Online (Sandbox Code Playgroud)

这一切都很好。但是假设我有一个函数parser从上面创建并返回父解析器,但不能直接访问子解析器。

我怎样才能打印出子解析器参数的默认值?或者分别获取每个子解析器的句柄?

In [8]: parser._subparsers._defaults
Out[8]: {}

In [9]: parser._subparsers.get_default("other_test")  # is None
Run Code Online (Sandbox Code Playgroud)

似乎没有从任何更多的属性或方法,parser._subparsersparser可能显示的默认值。

总体问题是:当您只有父解析器的句柄时,如何以编程方式访问子解析器默认值?

python parsing arguments argparse subparsers

0
推荐指数
1
解决办法
2590
查看次数

我可以将参数名称作为参数传递吗?

让说我有一个函数F有两个可选参数par1par2.

我想写一个新函数:

def some_plot(parameter):
    array = []
    for n in range(100):
        array.append(F(parameter = n))
    # make some plot using array
Run Code Online (Sandbox Code Playgroud)

然后我想通过调用some_plot('par1')(或some_plot(par1))分割错误来制作一些情节.

是否可以将参数的名称作为参数传递?

python parameters arguments parameter-passing

0
推荐指数
2
解决办法
50
查看次数

Java方法需要参数,即使它没有被使用

所以,我在Android Studio中搞乱了,我有一个看起来像这样的方法:

public void check(View view) {
        EditText numberEditText = findViewById(R.id.number);
        int number = Integer.parseInt(numberEditText.getText().toString());
        Toast.makeText(this, getMessage(number), Toast.LENGTH_SHORT).show();
    }
Run Code Online (Sandbox Code Playgroud)

按下按钮时调用该方法,并按预期工作.但是,当我从方法中删除参数时,请离开

public void check() {
        EditText numberEditText = findViewById(R.id.number);
        int number = Integer.parseInt(numberEditText.getText().toString());
        Toast.makeText(this, getMessage(number), Toast.LENGTH_SHORT).show();
    },
Run Code Online (Sandbox Code Playgroud)

按下按钮时应用程序崩溃.我发现这很奇怪,因为我实际上并没有使用View传递的东西.我认为这是View传递的一些问题,我没有采取任何论据.有没有办法绕过View这个方法?我认为当方法采用它不使用的参数时,它会使代码更难以阅读.

这是堆栈跟踪:

08-08 19:37:55.120 11217-11217/com.example.corfi.numbershapes E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.corfi.numbershapes, PID: 11217
    java.lang.IllegalStateException: Could not find method check(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button3'
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:424) …
Run Code Online (Sandbox Code Playgroud)

java methods android arguments

0
推荐指数
1
解决办法
86
查看次数

为什么当在子vb6上传递参数时我检索错误?

我写了这段代码:

Sub Insert_Pic_From_File2(PicPath As String, ByVal row As Integer, ByVal col As Integer)


Dim Pic As Picture, Sh As Shape, Rng As Range

Set Rng = Range.Cells(row, col)
Set Rng = Rng.MergeArea

With Rng
    Set Sh = ActiveSheet.Shapes.AddPicture(Filename:=PicPath, linkToFile:=msoFalse, SaveWithDocument:=msoTrue, Left:=.Left, Top:=.Top, Width:=.Width, Height:=.Height)
    Sh.LockAspectRatio = msoFalse
End With

Set Sh = Nothing
Set Rng = Nothing

End Sub
Run Code Online (Sandbox Code Playgroud)

我从这行说出来:

Insert_Pic_From_File2 ("D:\Area Open\ok.png", y, col_result)
Run Code Online (Sandbox Code Playgroud)

y和col_result都是整数.

当我按下输入程序时出现此错误(意大利语)

errore di compilazione:Previsto:=

我觉得用英语说:

编译错误:需要:=

为什么这个?如果我删除参数y和col_result似乎没有问题,但没有参数.

非常感谢.

vb6 arguments

0
推荐指数
1
解决办法
37
查看次数

Laravel:传递给函数0的参数太少,预期恰好有1个”

我知道这已经被问过了。香港专业教育学院看到的问题和答案,但找不到我可以得到帮助的东西。

我正在尝试将简单数据传递到数据库中。到目前为止,一切都很好。我认为当我尝试添加userID时会出现问题,因为必须从Auth中提取该ID。所以这是我的控制器代码。

端节点,userID来自称为用户的外部表。和userID将用作外键。在userstable中称为“ id”

    <?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ShopController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index()
    {
        return view('/shop/addShop');
    }


    protected function validator(array $data)
    {
        return Validator::make($data, [
            'userid' => ['required', 'string', 'max:255'],
            'shopname' => ['required', 'string', 'max:255'],
            'address' => ['required', 'string', 'max:255'],
            'postal' => ['required', 'string', 'max:255'],
            'city' => ['required', 'string', 'max:255'],
            'phone' => ['required', 'string', 'max:255'],
        ]);
    }

    /** …
Run Code Online (Sandbox Code Playgroud)

arguments laravel

0
推荐指数
1
解决办法
3770
查看次数

为什么在函数声明中传递const指针参数无效

问题描述

我有一个函数,它接受一个指向const double数组的const指针,在该数组上我使用大小作为my_func内的边界检查进行循环。

在.h中声明

void my_func(const double * const my_array, size_t size);
Run Code Online (Sandbox Code Playgroud)

在.c中实现

void my_func(const double * const my_array, size_t size) {
  for (size_t idx = 0; idx < size; idx++) {
    do_something_with(my_array[idx]);
  }
}
Run Code Online (Sandbox Code Playgroud)

我不希望在函数内部更改指针,从而使其成为现实* const。我不希望它指向的数据被更改,因此const double

但是Clang-Tidy希望我让函数声明将const放到指针上,使其变为

void my_func(const double * my_array, size_t size);
Run Code Online (Sandbox Code Playgroud)

Clang-Tidy:参数'my_array'在函数声明中是const限定的;参数的const限定仅在函数定义中起作用

如果我遵循该建议,但又希望保持上面的约束,那么我的函数声明和定义将不再相同。

问题

1)
假设我的指针参数不是const(const double * pointer_arg),如果我pointer_arg在函数内部进行更改以指向另一个const double,则该更改在函数外部是否可见?也就是说,在执行完我的函数的那一行之后,填充pointer_arg指向何处?如果看不见,是否表示指针已按值复制?这有可能吗?

2)
声明声明中的const无效的决定背后的原因是什么?

3)
在声明和定义中使用不同的函数签名可能会有什么好处?对我来说,看我的头文件还不清楚实现的工作原理,函数内部是否为const,不是吗?这让我或代码合作者感到困惑。

4)
我应该在声明中删除const吗?什么是最好的做法,在这种情况下?

c pointers arguments

0
推荐指数
1
解决办法
232
查看次数

如何使用std :: allocator_traits :: construct从函数参数创建结构对象?

我有一个结构定义为:

struct Foo
{
    double x;
    double y;
};
Run Code Online (Sandbox Code Playgroud)

我有一个函数将此结构作为参数:

void doSomethingWithFoo(Foo foo);
Run Code Online (Sandbox Code Playgroud)

我还想通过直接传递struct成员来使用此功能:

void doSomethingWithFoo(double x, double y);
Run Code Online (Sandbox Code Playgroud)

std::allocator_traits::construct没有明确定义第二个功能,是否有一种使用方式或任何其他方式能够做到这一点?

c++ struct arguments function parameter-passing

0
推荐指数
1
解决办法
35
查看次数