···33 SPDX-License-Identifier: ISC
44---------------------------------------------------------------------------*)
5566-let () = exit (Cmdliner.Cmd.eval Publicsuffix_cmd.default_cmd)
66+open Cmdliner
77+88+let psl = lazy (Publicsuffix.create ())
99+1010+let psl () = Lazy.force psl
1111+1212+(* Helper functions for printing results *)
1313+1414+let print_error e =
1515+ Printf.printf "ERROR: %s\n" (Publicsuffix.error_to_string e)
1616+1717+let print_result = function
1818+ | Ok s -> print_endline s
1919+ | Error e -> print_error e
2020+2121+let print_bool_result = function
2222+ | Ok b -> print_endline (string_of_bool b)
2323+ | Error e -> print_error e
2424+2525+let print_result_with_section = function
2626+ | Ok (s, sec) ->
2727+ let sec_str = match sec with
2828+ | Publicsuffix.ICANN -> "ICANN"
2929+ | Publicsuffix.Private -> "PRIVATE"
3030+ in
3131+ Printf.printf "%s (%s)\n" s sec_str
3232+ | Error e -> print_error e
3333+3434+let registrable_cmd =
3535+ let doc = "Get the registrable domain for a given domain" in
3636+ let info = Cmd.info "registrable" ~doc in
3737+ let term =
3838+ Term.(const print_result $ Publicsuffix_cmd.registrable_term (psl ()))
3939+ in
4040+ Cmd.v info term
4141+4242+let suffix_cmd =
4343+ let doc = "Get the public suffix for a given domain" in
4444+ let info = Cmd.info "suffix" ~doc in
4545+ let term =
4646+ Term.(const print_result $ Publicsuffix_cmd.suffix_term (psl ()))
4747+ in
4848+ Cmd.v info term
4949+5050+let is_suffix_cmd =
5151+ let doc = "Check if a domain is a public suffix" in
5252+ let info = Cmd.info "is_suffix" ~doc in
5353+ let term =
5454+ Term.(const print_bool_result $ Publicsuffix_cmd.is_suffix_term (psl ()))
5555+ in
5656+ Cmd.v info term
5757+5858+let is_registrable_cmd =
5959+ let doc = "Check if a domain is a registrable domain" in
6060+ let info = Cmd.info "is_registrable" ~doc in
6161+ let term =
6262+ Term.(const print_bool_result $ Publicsuffix_cmd.is_registrable_term (psl ()))
6363+ in
6464+ Cmd.v info term
6565+6666+let registrable_section_cmd =
6767+ let doc = "Get the registrable domain with section information" in
6868+ let info = Cmd.info "registrable_section" ~doc in
6969+ let term =
7070+ Term.(const print_result_with_section
7171+ $ Publicsuffix_cmd.registrable_section_term (psl ()))
7272+ in
7373+ Cmd.v info term
7474+7575+let suffix_section_cmd =
7676+ let doc = "Get the public suffix with section information" in
7777+ let info = Cmd.info "suffix_section" ~doc in
7878+ let term =
7979+ Term.(const print_result_with_section
8080+ $ Publicsuffix_cmd.suffix_section_term (psl ()))
8181+ in
8282+ Cmd.v info term
8383+8484+let stats_cmd =
8585+ let doc = "Print statistics about the Public Suffix List" in
8686+ let info = Cmd.info "stats" ~doc in
8787+ let term =
8888+ Term.(const (fun (total, icann, private_rules) ->
8989+ Printf.printf "Total rules: %d\n" total;
9090+ Printf.printf "ICANN rules: %d\n" icann;
9191+ Printf.printf "Private rules: %d\n" private_rules)
9292+ $ Publicsuffix_cmd.stats_term (psl ()))
9393+ in
9494+ Cmd.v info term
9595+9696+let default_cmd =
9797+ let doc = "Query the Public Suffix List" in
9898+ let sdocs = Manpage.s_common_options in
9999+ let info = Cmd.info "publicsuffix" ~version:"%%VERSION%%" ~doc ~sdocs in
100100+ Cmd.group info [
101101+ registrable_cmd;
102102+ suffix_cmd;
103103+ is_suffix_cmd;
104104+ is_registrable_cmd;
105105+ registrable_section_cmd;
106106+ suffix_section_cmd;
107107+ stats_cmd;
108108+ ]
109109+110110+let () = exit (Cmd.eval default_cmd)
+27-104
lib/cmd/publicsuffix_cmd.ml
···5566open Cmdliner
7788-let psl = lazy (Publicsuffix.create ())
99-1010-let print_error e =
1111- Printf.printf "ERROR: %s\n" (Publicsuffix.error_to_string e)
1212-1313-let print_result = function
1414- | Ok s -> print_endline s
1515- | Error e -> print_error e
1616-1717-let print_bool_result = function
1818- | Ok b -> print_endline (string_of_bool b)
1919- | Error e -> print_error e
2020-2121-let print_result_with_section = function
2222- | Ok (s, sec) ->
2323- let sec_str = match sec with
2424- | Publicsuffix.ICANN -> "ICANN"
2525- | Publicsuffix.Private -> "PRIVATE"
2626- in
2727- Printf.printf "%s (%s)\n" s sec_str
2828- | Error e -> print_error e
2929-3030-(* Common arguments *)
88+(* Argument terms *)
3193210let domain_arg =
3311 let doc = "The domain name to query." in
3412 Arg.(required & pos 0 (some string) None & info [] ~docv:"DOMAIN" ~doc)
35133636-(* Commands *)
1414+(* Term functions *)
37153838-let registrable_cmd =
3939- let doc = "Get the registrable domain for a given domain" in
4040- let info = Cmd.info "registrable" ~doc in
4141- let term =
4242- Term.(const (fun domain ->
4343- print_result (Publicsuffix.registrable_domain (Lazy.force psl) domain))
4444- $ domain_arg)
4545- in
4646- Cmd.v info term
1616+let registrable_term psl =
1717+ Term.(const (fun domain -> Publicsuffix.registrable_domain psl domain)
1818+ $ domain_arg)
47194848-let suffix_cmd =
4949- let doc = "Get the public suffix for a given domain" in
5050- let info = Cmd.info "suffix" ~doc in
5151- let term =
5252- Term.(const (fun domain ->
5353- print_result (Publicsuffix.public_suffix (Lazy.force psl) domain))
5454- $ domain_arg)
5555- in
5656- Cmd.v info term
2020+let suffix_term psl =
2121+ Term.(const (fun domain -> Publicsuffix.public_suffix psl domain)
2222+ $ domain_arg)
57235858-let is_suffix_cmd =
5959- let doc = "Check if a domain is a public suffix" in
6060- let info = Cmd.info "is_suffix" ~doc in
6161- let term =
6262- Term.(const (fun domain ->
6363- print_bool_result (Publicsuffix.is_public_suffix (Lazy.force psl) domain))
6464- $ domain_arg)
6565- in
6666- Cmd.v info term
2424+let is_suffix_term psl =
2525+ Term.(const (fun domain -> Publicsuffix.is_public_suffix psl domain)
2626+ $ domain_arg)
67276868-let is_registrable_cmd =
6969- let doc = "Check if a domain is a registrable domain" in
7070- let info = Cmd.info "is_registrable" ~doc in
7171- let term =
7272- Term.(const (fun domain ->
7373- print_bool_result (Publicsuffix.is_registrable_domain (Lazy.force psl) domain))
7474- $ domain_arg)
7575- in
7676- Cmd.v info term
2828+let is_registrable_term psl =
2929+ Term.(const (fun domain -> Publicsuffix.is_registrable_domain psl domain)
3030+ $ domain_arg)
77317878-let registrable_section_cmd =
7979- let doc = "Get the registrable domain with section information" in
8080- let info = Cmd.info "registrable_section" ~doc in
8181- let term =
8282- Term.(const (fun domain ->
8383- print_result_with_section (Publicsuffix.registrable_domain_with_section (Lazy.force psl) domain))
8484- $ domain_arg)
8585- in
8686- Cmd.v info term
3232+let registrable_section_term psl =
3333+ Term.(const (fun domain ->
3434+ Publicsuffix.registrable_domain_with_section psl domain)
3535+ $ domain_arg)
87368888-let suffix_section_cmd =
8989- let doc = "Get the public suffix with section information" in
9090- let info = Cmd.info "suffix_section" ~doc in
9191- let term =
9292- Term.(const (fun domain ->
9393- print_result_with_section (Publicsuffix.public_suffix_with_section (Lazy.force psl) domain))
9494- $ domain_arg)
9595- in
9696- Cmd.v info term
3737+let suffix_section_term psl =
3838+ Term.(const (fun domain -> Publicsuffix.public_suffix_with_section psl domain)
3939+ $ domain_arg)
97409898-let stats_cmd =
9999- let doc = "Print statistics about the Public Suffix List" in
100100- let info = Cmd.info "stats" ~doc in
101101- let term =
102102- Term.(const (fun () ->
103103- let psl = Lazy.force psl in
104104- Printf.printf "Total rules: %d\n" (Publicsuffix.rule_count psl);
105105- Printf.printf "ICANN rules: %d\n" (Publicsuffix.icann_rule_count psl);
106106- Printf.printf "Private rules: %d\n" (Publicsuffix.private_rule_count psl))
107107- $ const ())
108108- in
109109- Cmd.v info term
110110-111111-let default_cmd =
112112- let doc = "Query the Public Suffix List" in
113113- let sdocs = Manpage.s_common_options in
114114- let info = Cmd.info "publicsuffix" ~version:"%%VERSION%%" ~doc ~sdocs in
115115- Cmd.group info [
116116- registrable_cmd;
117117- suffix_cmd;
118118- is_suffix_cmd;
119119- is_registrable_cmd;
120120- registrable_section_cmd;
121121- suffix_section_cmd;
122122- stats_cmd;
123123- ]
4141+let stats_term psl =
4242+ Term.(const (fun () ->
4343+ (Publicsuffix.rule_count psl,
4444+ Publicsuffix.icann_rule_count psl,
4545+ Publicsuffix.private_rule_count psl))
4646+ $ const ())
+31-20
lib/cmd/publicsuffix_cmd.mli
···33 SPDX-License-Identifier: ISC
44---------------------------------------------------------------------------*)
5566-(** Command-line interface terms for the publicsuffix library.
66+(** Reusable Cmdliner terms for the publicsuffix library.
7788- This module provides Cmdliner terms that can be used to build
99- command-line tools that work with the Public Suffix List. *)
88+ This module provides argument parsers and term functions that can be
99+ composed to build command-line tools that work with the Public Suffix List. *)
10101111-(** {1 Command terms} *)
1111+(** {1 Argument terms} *)
12121313-val registrable_cmd : unit Cmdliner.Cmd.t
1414-(** Command to get the registrable domain for a given domain. *)
1313+val domain_arg : string Cmdliner.Term.t
1414+(** Cmdliner term for parsing a domain name from a positional argument. *)
15151616-val suffix_cmd : unit Cmdliner.Cmd.t
1717-(** Command to get the public suffix for a given domain. *)
1616+(** {1 Term functions} *)
1717+1818+val registrable_term :
1919+ Publicsuffix.t -> (string, Publicsuffix.error) result Cmdliner.Term.t
2020+(** Term that gets the registrable domain for a given domain. *)
18211919-val is_suffix_cmd : unit Cmdliner.Cmd.t
2020-(** Command to check if a domain is a public suffix. *)
2222+val suffix_term :
2323+ Publicsuffix.t -> (string, Publicsuffix.error) result Cmdliner.Term.t
2424+(** Term that gets the public suffix for a given domain. *)
21252222-val is_registrable_cmd : unit Cmdliner.Cmd.t
2323-(** Command to check if a domain is a registrable domain. *)
2626+val is_suffix_term :
2727+ Publicsuffix.t -> (bool, Publicsuffix.error) result Cmdliner.Term.t
2828+(** Term that checks if a domain is a public suffix. *)
24292525-val registrable_section_cmd : unit Cmdliner.Cmd.t
2626-(** Command to get the registrable domain with section information. *)
3030+val is_registrable_term :
3131+ Publicsuffix.t -> (bool, Publicsuffix.error) result Cmdliner.Term.t
3232+(** Term that checks if a domain is a registrable domain. *)
27332828-val suffix_section_cmd : unit Cmdliner.Cmd.t
2929-(** Command to get the public suffix with section information. *)
3434+val registrable_section_term :
3535+ Publicsuffix.t ->
3636+ (string * Publicsuffix.section, Publicsuffix.error) result Cmdliner.Term.t
3737+(** Term that gets the registrable domain with section information. *)
30383131-val stats_cmd : unit Cmdliner.Cmd.t
3232-(** Command to print statistics about the Public Suffix List. *)
3939+val suffix_section_term :
4040+ Publicsuffix.t ->
4141+ (string * Publicsuffix.section, Publicsuffix.error) result Cmdliner.Term.t
4242+(** Term that gets the public suffix with section information. *)
33433434-val default_cmd : unit Cmdliner.Cmd.t
3535-(** The default command that groups all subcommands. *)
4444+val stats_term : Publicsuffix.t -> (int * int * int) Cmdliner.Term.t
4545+(** Term that returns statistics about the Public Suffix List as a tuple of
4646+ (total_rules, icann_rules, private_rules). *)