OCaml文盲需要记录处理帮助
这有点尴尬,但我已经看过一个OCaml程序,我知道我想做什么,但我找不到文档来帮助我用正确的语法写我想要的东西,因为我以前从未使用过这种语言 我对程序做了很多工作上的更改,而这恰好是我需要做的最后一次更改,我无法完成它 有一个名为OCaml文盲需要记录处理帮助,ocaml,command-line-arguments,record,Ocaml,Command Line Arguments,Record,这有点尴尬,但我已经看过一个OCaml程序,我知道我想做什么,但我找不到文档来帮助我用正确的语法写我想要的东西,因为我以前从未使用过这种语言 我对程序做了很多工作上的更改,而这恰好是我需要做的最后一次更改,我无法完成它 有一个名为caps\u default的排序记录(所使用的语法使我无法使用): let caps_default = { browserName = Firefox ; version = "" ; platform = A
caps\u default
的排序记录(所使用的语法使我无法使用):
let caps_default =
{
browserName = Firefox ;
version = "" ;
platform = ANY ;
username = "" ;
accessKey = "" ;
javascriptEnabled = true
}
;;
然后定义这些变量以接收命令行参数:
exception Incorrect_cmdline of string option
;;
let browser_name = ref ""
and hub_url_opt = ref ""
and target_url_opt = ref ""
and xml_out_string = ref ""
and user_name = ref ""
and access_key = ref ""
;;
我添加了两个名为“username”和“accessKey”的新参数,我想将它们添加到“caps”记录中
继续,这里解析命令行参数:
let (target_url, hub_url, caps, xml_out) =
try
parse_cmdline
[
( noshort , "browser-name" , None, (atmost_once browser_name (Incorrect_cmdline (Some "browser-name"))) ) ;
( noshort , "hub-url" , None, (atmost_once hub_url_opt (Incorrect_cmdline (Some "hub-url"))) ) ;
( noshort , "target-url" , None, (atmost_once target_url_opt (Incorrect_cmdline (Some "target-url"))) ) ;
( noshort , "xml-output" , None, (atmost_once xml_out_string (Incorrect_cmdline (Some "xml-output"))) ) ;
( noshort , "username" , None, (atmost_once user_name (Incorrect_cmdline (Some "username"))) ) ;
( noshort , "accessKey" , None, (atmost_once access_key (Incorrect_cmdline (Some "accessKey"))) ) ;
( noshort , "help" , Some (fun s -> raise (Incorrect_cmdline None)) , None );
]
( fun x -> raise (Incorrect_cmdline None)) ;
ignore (List.map
(fun s -> if BatString.is_empty !s then raise (Incorrect_cmdline None) else () )
[ browser_name ; hub_url_opt ; target_url_opt ] );
let target_url =
try
Neturl.parse_url !target_url_opt
with
Neturl.Malformed_URL ->
raise (Incorrect_cmdline (Some ("Invalid target url: " ^ !target_url_opt)))
and hub_url =
try
Neturl.parse_url !hub_url_opt
with
Neturl.Malformed_URL ->
raise (Incorrect_cmdline (Some ("Invalid hub url: " ^ !hub_url_opt)))
and xml_out =
if BatString.is_empty !xml_out_string
then fun s -> ignore s
else
let chan = open_out !xml_out_string in
(fun xml ->
output_string chan (Xml.to_string xml) ;
close_out chan )
(* Continued below *)
我希望将新的命令行参数选项username
和accessKey
,分配给变量user\u name
和access\u key
,传输到下面的let分配中定义的caps
记录:
(* Continued from above *)
and caps =
{ caps_default with username = !user_name } (* My addition *)
{ caps_default with accessKey = !access_key } (* My addition *)
match !browser_name with (* This switch is original and worked *)
| "firefox" -> { caps_default with browserName = Firefox }
| "chrome" -> { caps_default with browserName = Chrome }
| "html_unit" -> { caps_default with browserName = HtmlUnit }
| "ie" -> { caps_default with browserName = InternetExplorer }
| "iphone" -> { caps_default with browserName = IPhone }
| _ -> raise (Incorrect_cmdline (Some ("Invalid browser name: " ^ !browser_name)))
in
(target_url, hub_url, caps, xml_out)
with
Incorrect_cmdline arg ->
(match arg with | None -> () | Some msg -> print_endline msg) ;
print_synopsis () ;
exit 48
;; (* End of relevant code *)
在实验中,我可以注释掉原作者对--browser name的切换,并通过用大括号括起来的庄严线条指定一个值:
and caps=
{ caps_default with browserName = Chrome } (* Hard-coding a Chrome choice *)
或
或
有人能给我指出正确的方向吗?我想保留原作者编写的开关,但还要将用户名
和访问键
字符串传输到caps
记录。我可以稍后更改caps
对象吗?我是否可以事先修改默认\u caps
,然后将其复制到caps
此代码每次执行只运行一次,因此修改default\u caps
对象绝对没有关系,即使这样做不太合适
谢谢你的帮助
更新
非常感谢斯科菲尔德先生迄今为止的协助。奇怪的是,我现在遇到了一个稍微不同的错误,抱怨我只能假设是类型的错误匹配。以下是错误的内容:
File "tester.ml", line 110, characters 40-47:
Error: This expression has type Selenium.Json_client.capabilities
but an expression was expected of type Selenium.Json_client.browser
第110行是
{ caps_default with browserName = browser;
在斯科菲尔德先生的代码片段中,40-47是browser
default\u caps
的类型功能的定义如下:
type capabilities =
{
browserName : browser ;
version : string ;
platform : platform ;
javascriptEnabled : bool ;
username : string ;
accessKey : string ;
}
同样,类型浏览器
:
type browser =
Chrome | Firefox | HtmlUnit | InternetExplorer | IPhone
如果重要的话,mli(接口,正确吗?)定义是相同的
更新第2部分
只需进行少量修改,即可简化以下代码:
match !browser_name with
| "firefox" -> { caps_default with browserName = Firefox }
| "chrome" -> { caps_default with browserName = Chrome }
| "html_unit" -> { caps_default with browserName = HtmlUnit }
| "ie" -> { caps_default with browserName = InternetExplorer }
| "iphone" -> { caps_default with browserName = IPhone }
| _ -> raise (Incorrect_cmdline (Some ("Invalid browser name: " ^ !browser_name)))
为此:
match !browser_name with
| "firefox" -> Firefox
| "chrome" -> Chrome
| "html_unit" -> HtmlUnit
| "ie" -> InternetExplorer
| "iphone" -> IPhone
| _ -> raise (Incorrect_cmdline (Some ("Invalid browser name: " ^ !browser_name)))
在继续之前:
in
{ caps_default with browserName = browser;
username = !user_name;
accessKey = !access_key;
}
要做到这一点,您可能需要学习更多的OCaml:-)
名为caps\u default
的东西是一个记录,即它有一个记录类型。您必须找到此记录类型的定义,并向其中添加新功能
更新
好的,你已经把记录类型修好了。这里有一些代码
and caps =
let browser =
match !browser_name with
| "firefox" -> Firefox
| "chrome" -> Chrome
| "html_unit" -> HtmlUnit
| "ie" -> InternetExplorer
| "iphone" -> IPhone
| _ ->
raise (Incorrect_cmdline
(Some ("Invalid browser name: " ^ !browser_name)))
in
{ caps_default with browserName = browser;
username = !user_name;
accessKey = !access_key;
}
in
(target_url, hub_url, caps, xml_out)
(不用说,这是未经测试的代码。)
更新2
(修复了用户名的拼写,谢谢。)啊,我怀疑这是一条记录,我已经这么做了,谢天谢地,我给了他们两种字符串类型。唉,我仍然不知道如何正确地将收到的命令行参数分配给caps
,并维护现有的开关。到目前为止,这是一个很大的帮助。但奇怪的是,我又遇到了一个关于类型不匹配的问题。我已经在最后用新的部分更新了原始问题。我的原始代码有错误。再次查看上面的代码。(对不起)哈哈!是的,我知道哪里出了问题。非常感谢你,斯科菲尔德先生,这似乎很适合我的需要。我会支持你的答案,但显然我还需要两个代表来做,哈。PS:另一个微小的修正是!用户名
应该是什么时候的
!用户名
,供以后可能会将此作为指南的任何人使用。
in
{ caps_default with browserName = browser;
username = !user_name;
accessKey = !access_key;
}
and caps =
let browser =
match !browser_name with
| "firefox" -> Firefox
| "chrome" -> Chrome
| "html_unit" -> HtmlUnit
| "ie" -> InternetExplorer
| "iphone" -> IPhone
| _ ->
raise (Incorrect_cmdline
(Some ("Invalid browser name: " ^ !browser_name)))
in
{ caps_default with browserName = browser;
username = !user_name;
accessKey = !access_key;
}
in
(target_url, hub_url, caps, xml_out)