OCaml implementation of the Mozilla Public Suffix service

refine cmdliner

+163 -125
+105 -1
bin/main.ml
··· 3 3 SPDX-License-Identifier: ISC 4 4 ---------------------------------------------------------------------------*) 5 5 6 - let () = exit (Cmdliner.Cmd.eval Publicsuffix_cmd.default_cmd) 6 + open Cmdliner 7 + 8 + let psl = lazy (Publicsuffix.create ()) 9 + 10 + let psl () = Lazy.force psl 11 + 12 + (* Helper functions for printing results *) 13 + 14 + let print_error e = 15 + Printf.printf "ERROR: %s\n" (Publicsuffix.error_to_string e) 16 + 17 + let print_result = function 18 + | Ok s -> print_endline s 19 + | Error e -> print_error e 20 + 21 + let print_bool_result = function 22 + | Ok b -> print_endline (string_of_bool b) 23 + | Error e -> print_error e 24 + 25 + let print_result_with_section = function 26 + | Ok (s, sec) -> 27 + let sec_str = match sec with 28 + | Publicsuffix.ICANN -> "ICANN" 29 + | Publicsuffix.Private -> "PRIVATE" 30 + in 31 + Printf.printf "%s (%s)\n" s sec_str 32 + | Error e -> print_error e 33 + 34 + let registrable_cmd = 35 + let doc = "Get the registrable domain for a given domain" in 36 + let info = Cmd.info "registrable" ~doc in 37 + let term = 38 + Term.(const print_result $ Publicsuffix_cmd.registrable_term (psl ())) 39 + in 40 + Cmd.v info term 41 + 42 + let suffix_cmd = 43 + let doc = "Get the public suffix for a given domain" in 44 + let info = Cmd.info "suffix" ~doc in 45 + let term = 46 + Term.(const print_result $ Publicsuffix_cmd.suffix_term (psl ())) 47 + in 48 + Cmd.v info term 49 + 50 + let is_suffix_cmd = 51 + let doc = "Check if a domain is a public suffix" in 52 + let info = Cmd.info "is_suffix" ~doc in 53 + let term = 54 + Term.(const print_bool_result $ Publicsuffix_cmd.is_suffix_term (psl ())) 55 + in 56 + Cmd.v info term 57 + 58 + let is_registrable_cmd = 59 + let doc = "Check if a domain is a registrable domain" in 60 + let info = Cmd.info "is_registrable" ~doc in 61 + let term = 62 + Term.(const print_bool_result $ Publicsuffix_cmd.is_registrable_term (psl ())) 63 + in 64 + Cmd.v info term 65 + 66 + let registrable_section_cmd = 67 + let doc = "Get the registrable domain with section information" in 68 + let info = Cmd.info "registrable_section" ~doc in 69 + let term = 70 + Term.(const print_result_with_section 71 + $ Publicsuffix_cmd.registrable_section_term (psl ())) 72 + in 73 + Cmd.v info term 74 + 75 + let suffix_section_cmd = 76 + let doc = "Get the public suffix with section information" in 77 + let info = Cmd.info "suffix_section" ~doc in 78 + let term = 79 + Term.(const print_result_with_section 80 + $ Publicsuffix_cmd.suffix_section_term (psl ())) 81 + in 82 + Cmd.v info term 83 + 84 + let stats_cmd = 85 + let doc = "Print statistics about the Public Suffix List" in 86 + let info = Cmd.info "stats" ~doc in 87 + let term = 88 + Term.(const (fun (total, icann, private_rules) -> 89 + Printf.printf "Total rules: %d\n" total; 90 + Printf.printf "ICANN rules: %d\n" icann; 91 + Printf.printf "Private rules: %d\n" private_rules) 92 + $ Publicsuffix_cmd.stats_term (psl ())) 93 + in 94 + Cmd.v info term 95 + 96 + let default_cmd = 97 + let doc = "Query the Public Suffix List" in 98 + let sdocs = Manpage.s_common_options in 99 + let info = Cmd.info "publicsuffix" ~version:"%%VERSION%%" ~doc ~sdocs in 100 + Cmd.group info [ 101 + registrable_cmd; 102 + suffix_cmd; 103 + is_suffix_cmd; 104 + is_registrable_cmd; 105 + registrable_section_cmd; 106 + suffix_section_cmd; 107 + stats_cmd; 108 + ] 109 + 110 + let () = exit (Cmd.eval default_cmd)
+27 -104
lib/cmd/publicsuffix_cmd.ml
··· 5 5 6 6 open Cmdliner 7 7 8 - let psl = lazy (Publicsuffix.create ()) 9 - 10 - let print_error e = 11 - Printf.printf "ERROR: %s\n" (Publicsuffix.error_to_string e) 12 - 13 - let print_result = function 14 - | Ok s -> print_endline s 15 - | Error e -> print_error e 16 - 17 - let print_bool_result = function 18 - | Ok b -> print_endline (string_of_bool b) 19 - | Error e -> print_error e 20 - 21 - let print_result_with_section = function 22 - | Ok (s, sec) -> 23 - let sec_str = match sec with 24 - | Publicsuffix.ICANN -> "ICANN" 25 - | Publicsuffix.Private -> "PRIVATE" 26 - in 27 - Printf.printf "%s (%s)\n" s sec_str 28 - | Error e -> print_error e 29 - 30 - (* Common arguments *) 8 + (* Argument terms *) 31 9 32 10 let domain_arg = 33 11 let doc = "The domain name to query." in 34 12 Arg.(required & pos 0 (some string) None & info [] ~docv:"DOMAIN" ~doc) 35 13 36 - (* Commands *) 14 + (* Term functions *) 37 15 38 - let registrable_cmd = 39 - let doc = "Get the registrable domain for a given domain" in 40 - let info = Cmd.info "registrable" ~doc in 41 - let term = 42 - Term.(const (fun domain -> 43 - print_result (Publicsuffix.registrable_domain (Lazy.force psl) domain)) 44 - $ domain_arg) 45 - in 46 - Cmd.v info term 16 + let registrable_term psl = 17 + Term.(const (fun domain -> Publicsuffix.registrable_domain psl domain) 18 + $ domain_arg) 47 19 48 - let suffix_cmd = 49 - let doc = "Get the public suffix for a given domain" in 50 - let info = Cmd.info "suffix" ~doc in 51 - let term = 52 - Term.(const (fun domain -> 53 - print_result (Publicsuffix.public_suffix (Lazy.force psl) domain)) 54 - $ domain_arg) 55 - in 56 - Cmd.v info term 20 + let suffix_term psl = 21 + Term.(const (fun domain -> Publicsuffix.public_suffix psl domain) 22 + $ domain_arg) 57 23 58 - let is_suffix_cmd = 59 - let doc = "Check if a domain is a public suffix" in 60 - let info = Cmd.info "is_suffix" ~doc in 61 - let term = 62 - Term.(const (fun domain -> 63 - print_bool_result (Publicsuffix.is_public_suffix (Lazy.force psl) domain)) 64 - $ domain_arg) 65 - in 66 - Cmd.v info term 24 + let is_suffix_term psl = 25 + Term.(const (fun domain -> Publicsuffix.is_public_suffix psl domain) 26 + $ domain_arg) 67 27 68 - let is_registrable_cmd = 69 - let doc = "Check if a domain is a registrable domain" in 70 - let info = Cmd.info "is_registrable" ~doc in 71 - let term = 72 - Term.(const (fun domain -> 73 - print_bool_result (Publicsuffix.is_registrable_domain (Lazy.force psl) domain)) 74 - $ domain_arg) 75 - in 76 - Cmd.v info term 28 + let is_registrable_term psl = 29 + Term.(const (fun domain -> Publicsuffix.is_registrable_domain psl domain) 30 + $ domain_arg) 77 31 78 - let registrable_section_cmd = 79 - let doc = "Get the registrable domain with section information" in 80 - let info = Cmd.info "registrable_section" ~doc in 81 - let term = 82 - Term.(const (fun domain -> 83 - print_result_with_section (Publicsuffix.registrable_domain_with_section (Lazy.force psl) domain)) 84 - $ domain_arg) 85 - in 86 - Cmd.v info term 32 + let registrable_section_term psl = 33 + Term.(const (fun domain -> 34 + Publicsuffix.registrable_domain_with_section psl domain) 35 + $ domain_arg) 87 36 88 - let suffix_section_cmd = 89 - let doc = "Get the public suffix with section information" in 90 - let info = Cmd.info "suffix_section" ~doc in 91 - let term = 92 - Term.(const (fun domain -> 93 - print_result_with_section (Publicsuffix.public_suffix_with_section (Lazy.force psl) domain)) 94 - $ domain_arg) 95 - in 96 - Cmd.v info term 37 + let suffix_section_term psl = 38 + Term.(const (fun domain -> Publicsuffix.public_suffix_with_section psl domain) 39 + $ domain_arg) 97 40 98 - let stats_cmd = 99 - let doc = "Print statistics about the Public Suffix List" in 100 - let info = Cmd.info "stats" ~doc in 101 - let term = 102 - Term.(const (fun () -> 103 - let psl = Lazy.force psl in 104 - Printf.printf "Total rules: %d\n" (Publicsuffix.rule_count psl); 105 - Printf.printf "ICANN rules: %d\n" (Publicsuffix.icann_rule_count psl); 106 - Printf.printf "Private rules: %d\n" (Publicsuffix.private_rule_count psl)) 107 - $ const ()) 108 - in 109 - Cmd.v info term 110 - 111 - let default_cmd = 112 - let doc = "Query the Public Suffix List" in 113 - let sdocs = Manpage.s_common_options in 114 - let info = Cmd.info "publicsuffix" ~version:"%%VERSION%%" ~doc ~sdocs in 115 - Cmd.group info [ 116 - registrable_cmd; 117 - suffix_cmd; 118 - is_suffix_cmd; 119 - is_registrable_cmd; 120 - registrable_section_cmd; 121 - suffix_section_cmd; 122 - stats_cmd; 123 - ] 41 + let stats_term psl = 42 + Term.(const (fun () -> 43 + (Publicsuffix.rule_count psl, 44 + Publicsuffix.icann_rule_count psl, 45 + Publicsuffix.private_rule_count psl)) 46 + $ const ())
+31 -20
lib/cmd/publicsuffix_cmd.mli
··· 3 3 SPDX-License-Identifier: ISC 4 4 ---------------------------------------------------------------------------*) 5 5 6 - (** Command-line interface terms for the publicsuffix library. 6 + (** Reusable Cmdliner terms for the publicsuffix library. 7 7 8 - This module provides Cmdliner terms that can be used to build 9 - command-line tools that work with the Public Suffix List. *) 8 + This module provides argument parsers and term functions that can be 9 + composed to build command-line tools that work with the Public Suffix List. *) 10 10 11 - (** {1 Command terms} *) 11 + (** {1 Argument terms} *) 12 12 13 - val registrable_cmd : unit Cmdliner.Cmd.t 14 - (** Command to get the registrable domain for a given domain. *) 13 + val domain_arg : string Cmdliner.Term.t 14 + (** Cmdliner term for parsing a domain name from a positional argument. *) 15 15 16 - val suffix_cmd : unit Cmdliner.Cmd.t 17 - (** Command to get the public suffix for a given domain. *) 16 + (** {1 Term functions} *) 17 + 18 + val registrable_term : 19 + Publicsuffix.t -> (string, Publicsuffix.error) result Cmdliner.Term.t 20 + (** Term that gets the registrable domain for a given domain. *) 18 21 19 - val is_suffix_cmd : unit Cmdliner.Cmd.t 20 - (** Command to check if a domain is a public suffix. *) 22 + val suffix_term : 23 + Publicsuffix.t -> (string, Publicsuffix.error) result Cmdliner.Term.t 24 + (** Term that gets the public suffix for a given domain. *) 21 25 22 - val is_registrable_cmd : unit Cmdliner.Cmd.t 23 - (** Command to check if a domain is a registrable domain. *) 26 + val is_suffix_term : 27 + Publicsuffix.t -> (bool, Publicsuffix.error) result Cmdliner.Term.t 28 + (** Term that checks if a domain is a public suffix. *) 24 29 25 - val registrable_section_cmd : unit Cmdliner.Cmd.t 26 - (** Command to get the registrable domain with section information. *) 30 + val is_registrable_term : 31 + Publicsuffix.t -> (bool, Publicsuffix.error) result Cmdliner.Term.t 32 + (** Term that checks if a domain is a registrable domain. *) 27 33 28 - val suffix_section_cmd : unit Cmdliner.Cmd.t 29 - (** Command to get the public suffix with section information. *) 34 + val registrable_section_term : 35 + Publicsuffix.t -> 36 + (string * Publicsuffix.section, Publicsuffix.error) result Cmdliner.Term.t 37 + (** Term that gets the registrable domain with section information. *) 30 38 31 - val stats_cmd : unit Cmdliner.Cmd.t 32 - (** Command to print statistics about the Public Suffix List. *) 39 + val suffix_section_term : 40 + Publicsuffix.t -> 41 + (string * Publicsuffix.section, Publicsuffix.error) result Cmdliner.Term.t 42 + (** Term that gets the public suffix with section information. *) 33 43 34 - val default_cmd : unit Cmdliner.Cmd.t 35 - (** The default command that groups all subcommands. *) 44 + val stats_term : Publicsuffix.t -> (int * int * int) Cmdliner.Term.t 45 + (** Term that returns statistics about the Public Suffix List as a tuple of 46 + (total_rules, icann_rules, private_rules). *)