command line arguments - OCaml illiterate in need of record manipulation assistance -
this little embarrassing i've been shown ocaml program know want cannot find documentation me write want in right syntax i've never used language before.
i made quite few working changes program , happens last change need make , cannot work out!
there record of sort (the syntax used had thrown me off) named caps_default
:
let caps_default = { browsername = firefox ; version = "" ; platform = ; username = "" ; accesskey = "" ; javascriptenabled = true } ;;
then these variables defined receive command line arguments:
exception incorrect_cmdline of string option ;; let browser_name = ref "" , hub_url_opt = ref "" , target_url_opt = ref "" , xml_out_string = ref "" , user_name = ref "" , access_key = ref "" ;;
i added 2 new arguments named 'username' , 'accesskey' , want add them "caps" record thing.
continuing on, here command line arguments parsed:
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" , (fun s -> raise (incorrect_cmdline none)) , none ); ] ( fun x -> raise (incorrect_cmdline none)) ; ignore (list.map (fun s -> if batstring.is_empty !s raise (incorrect_cmdline none) else () ) [ browser_name ; hub_url_opt ; target_url_opt ] ); let target_url = try neturl.parse_url !target_url_opt neturl.malformed_url -> raise (incorrect_cmdline (some ("invalid target url: " ^ !target_url_opt))) , hub_url = try neturl.parse_url !hub_url_opt neturl.malformed_url -> raise (incorrect_cmdline (some ("invalid hub url: " ^ !hub_url_opt))) , xml_out = if batstring.is_empty !xml_out_string 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 *)
my desire take new command line argument options username
, accesskey
, assigned variables user_name
, access_key
, transmitted caps
record defined in let assignments below:
(* continued above *) , caps = { caps_default username = !user_name } (* addition *) { caps_default accesskey = !access_key } (* addition *) match !browser_name (* switch original , worked *) | "firefox" -> { caps_default browsername = firefox } | "chrome" -> { caps_default browsername = chrome } | "html_unit" -> { caps_default browsername = htmlunit } | "ie" -> { caps_default browsername = internetexplorer } | "iphone" -> { caps_default browsername = iphone } | _ -> raise (incorrect_cmdline (some ("invalid browser name: " ^ !browser_name))) in (target_url, hub_url, caps, xml_out) incorrect_cmdline arg -> (match arg | none -> () | msg -> print_endline msg) ; print_synopsis () ; exit 48 ;; (* end of relevant code *)
experimenting comment out original author's switch --browser-name , assign single value putting solemn line surrounded curly braces:
and caps= { caps_default browsername = chrome } (* hard-coding chrome choice *)
or
and caps= { caps_default username = !user_name } (* sharing --username *)
or
and caps= { caps_default accesskey = !access_key } (* sharing --accesskey *)
can point me in correct direction? want keep switch original author wrote transmit username
, accesskey
strings caps
record on top of that. can change caps
object later? can modify default_caps
beforehand , copy caps
?
this code run once per execution, modifying default_caps
object of absolutely no concern, if it's not kosher so.
thanks assistance!
update
numerous mr. scofield's assistance far. curiously being met different error complaining can assume mis-match of type. here content of error:
file "tester.ml", line 110, characters 40-47: error: expression has type selenium.json_client.capabilities expression expected of type selenium.json_client.browser
line 110 is
{ caps_default browsername = browser;
in mr. scofield's code snippet, 40-47 being browser
.
the definition of default_caps
's type capabilities
such:
type capabilities = { browsername : browser ; version : string ; platform : platform ; javascriptenabled : bool ; username : string ; accesskey : string ; }
and similarly, type browser
:
type browser = chrome | firefox | htmlunit | internetexplorer | iphone
the mli (interface, correct?) definitions identical if important.
update part 2
minor correction needed simplify code this:
match !browser_name | "firefox" -> { caps_default browsername = firefox } | "chrome" -> { caps_default browsername = chrome } | "html_unit" -> { caps_default browsername = htmlunit } | "ie" -> { caps_default browsername = internetexplorer } | "iphone" -> { caps_default browsername = iphone } | _ -> raise (incorrect_cmdline (some ("invalid browser name: " ^ !browser_name)))
to this:
match !browser_name | "firefox" -> firefox | "chrome" -> chrome | "html_unit" -> htmlunit | "ie" -> internetexplorer | "iphone" -> iphone | _ -> raise (incorrect_cmdline (some ("invalid browser name: " ^ !browser_name)))
before continuing with:
in { caps_default browsername = browser; username = !user_name; accesskey = !access_key; }
you might have learn little more ocaml want to :-)
the thing named caps_default
record, i.e., has record type. you're going have find definition of record type , add new capabilities it.
update
ok, had record type fixed up. here's code
, caps = let browser = match !browser_name | "firefox" -> firefox | "chrome" -> chrome | "html_unit" -> htmlunit | "ie" -> internetexplorer | "iphone" -> iphone | _ -> raise (incorrect_cmdline (some ("invalid browser name: " ^ !browser_name))) in { caps_default browsername = browser; username = !user_name; accesskey = !access_key; } in (target_url, hub_url, caps, xml_out)
(needless say, untested code.)
update 2
(fixed spelling of user_name
, thanks.)
Comments
Post a Comment