Tag: 可选参数

getopt不parsing参数的可选参数

在C中,getopt_long不parsing命令行参数参数的可选参数。 当我运行该程序时,可选参数不被识别,如下面的示例运行。 $ ./respond –praise John Kudos to John $ ./respond –blame John You suck ! $ ./respond –blame You suck ! 这是testing代码。 #include <stdio.h> #include <getopt.h> int main(int argc, char ** argv ) { int getopt_ret, option_index; static struct option long_options[] = { {"praise", required_argument, 0, 'p'}, {"blame", optional_argument, 0, 'b'}, {0, 0, 0, […]

用于将默认参数值设置为默认构造函数的更好的语法

有人可能想用一个参数声明一个函数,并指定该参数的默认值是该types的默认构造函数的结果: void foo(a::really::long::type::name arg = a::really::long::type::name()); 有没有更好的语法,这不涉及inputtypes名称两次? 就像是: void foo(a::really::long::type::name arg = default); 我意识到我可以键入typedef的types名称,使其更漂亮,但我很好奇这样的语法是否存在。

Python:如何检查是否设置了可选的函数参数

在Python中有一种简单的方法来检查可选参数的值是否来自其默认值,或者是因为用户在函数调用中显式地设置了值吗?

是选项,并命名默认参数如油和水在斯卡拉API?

我正在研究一个Scala API(顺便说一下,Twilio),其中操作有相当多的参数,其中许多参数都有合理的默认值。 为了减less键入和增加可用性,我决定使用具有命名参数和默认参数的case类。 例如TwiML Gather动词: case class Gather(finishOnKey: Char = '#', numDigits: Int = Integer.MAX_VALUE, // Infinite callbackUrl: Option[String] = None, timeout: Int = 5 ) extends Verb 这里感兴趣的参数是callbackUrl 。 这是唯一真正可选的参数,如果没有提供任何值,就不会应用任何值(这是完全合法的)。 我已经声明它是一个选项,以便在API的实现端使用monadic map例程,但这会给API用户带来一些额外的负担: Gather(numDigits = 4, callbackUrl = Some("http://xxx")) // Should have been Gather(numDigits = 4, callbackUrl = "http://xxx") // Without the optional url, both […]

C#4.0:我可以使用一个颜色作为一个可选参数与默认值?

public void log(String msg, Color c = Color.black) { loggerText.ForeColor = c; loggerText.AppendText("\n" + msg); } 这会导致错误c必须是编译时常量。 我已经阅读了这一点,大多数的例子是处理string和整数。 我已经想通了,我可以使用colorconverter类,但我不知道这将是非常有效的。 有没有办法只传递一个基本的颜色作为一个可选的参数? public void log(String msg, String c = "Black") { ColorConverter conv = new ColorConverter(); Color color = (Color)conv.ConvertFromString(c); loggerText.ForeColor = color; loggerText.AppendText("\n" + msg); }

VB – 如何testing是否提供可选参数?

如何testing是否提供可选参数? – 在VB6 / VBA中 Function func (Optional ByRef arg As Variant = Nothing) If arg Is Nothing Then <—– run-time error 424 "object required" MsgBox "NOT SENT" End If End Function

具有默认值的选项的JavaScriptdevise模式?

// opt_options is optional function foo(a, b, opt_options) { // opt_c, opt_d, and opt_e are read from 'opt_options', only c and d have defaults var opt_c = 'default_for_c'; var opt_d = 'default_for_d'; var opt_e; // e has no default if (opt_options) { opt_c = opt_options.c || opt_c; opt_d = opt_options.d || opt_d; opt_e = opt_options.e; […]

AngularJS中的可选依赖项

我正在尝试在AngularJS中实现一个跨多个页面使用的控制器。 它利用一些服务。 其中一些在所有页面上加载,一些则不在。 我的意思是它是在不同的文件中定义的,并且这些文件是独立加载的。 但是,如果我不加载所有页面上的这些服务,我得到的错误: Error: Unknown provider: firstOtionalServiceProvider <- firstOtionalService 所以,我需要在所有页面上加载脚本。 我可以在Angular中声明依赖项作为可选项吗? 例如: myApp.controller('MyController', ['$scope', 'firstRequiredService', 'secondRequiredService', 'optional:firstOptionalService', 'optional:secondOptionalService', function($scope, firstRequiredService, secondRequiredService, firstOptionalService, secondOptionalSerivce){ // No need to check, as firstRequiredService must not be null firstRequiredService.alwaysDefined(); // If the dependency is not resolved i want Angular to set null as argument and check if […]

如何用可选参数创buildPython函数?

我有一个Python函数,它需要几个参数。 在某些情况下,其中一些论点可能会被忽略。 def some_function (self, a, b, c, d = None, e = None, f = None, g = None, h = None): #code 参数d到h是各自具有不同含义的string。 我可以select哪个可选参数以任意组合进行传递是非常重要的。 例如, (a, b, C, d, e)或(a, b, C, g, h)或(a, b, C, d, e, f或全部select)。 如果我可以重载函数,那将是非常好的 – 但是我读到Python不支持重载。 我试图在列表中插入一些所需的int参数 – 并得到一个参数不匹配的错误。 现在我发送空string作为占位符,而不是前几个缺less的参数。 我希望能够使用实际值来调用一个函数。 有没有办法做到这一点? 我可以传递一个列表而不是参数列表吗? 现在使用ctypes的原型如下所示: _fdll.some_function.argtypes = [c_void_p, […]

C#4.0,可选参数和参数不能一起工作

我如何创build一个具有可选参数和参数在一起的方法? static void Main(string[] args) { TestOptional("A",C: "D", "E");//this will not build TestOptional("A",C: "D"); //this does work , but i can only set 1 param Console.ReadLine(); } public static void TestOptional(string A, int B = 0, params string[] C) { Console.WriteLine(A); Console.WriteLine(B); Console.WriteLine(C.Count()); }