command_def.ml 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. (******************************************************************************)
  2. (* Copyright © Joly Clément, 2014-2015 *)
  3. (* *)
  4. (* leowzukw@vmail.me *)
  5. (* *)
  6. (* Ce logiciel est un programme informatique servant à exécuter *)
  7. (* automatiquement des programmes à l'ouverture du terminal. *)
  8. (* *)
  9. (* Ce logiciel est régi par la licence CeCILL soumise au droit français et *)
  10. (* respectant les principes de diffusion des logiciels libres. Vous pouvez *)
  11. (* utiliser, modifier et/ou redistribuer ce programme sous les conditions *)
  12. (* de la licence CeCILL telle que diffusée par le CEA, le CNRS et l'INRIA *)
  13. (* sur le site "http://www.cecill.info". *)
  14. (* *)
  15. (* En contrepartie de l'accessibilité au code source et des droits de copie, *)
  16. (* de modification et de redistribution accordés par cette licence, il n'est *)
  17. (* offert aux utilisateurs qu'une garantie limitée. Pour les mêmes raisons, *)
  18. (* seule une responsabilité restreinte pèse sur l'auteur du programme, le *)
  19. (* titulaire des droits patrimoniaux et les concédants successifs. *)
  20. (* *)
  21. (* A cet égard l'attention de l'utilisateur est attirée sur les risques *)
  22. (* associés au chargement, à l'utilisation, à la modification et/ou au *)
  23. (* développement et à la reproduction du logiciel par l'utilisateur étant *)
  24. (* donné sa spécificité de logiciel libre, qui peut le rendre complexe à *)
  25. (* manipuler et qui le réserve donc à des développeurs et des professionnels *)
  26. (* avertis possédant des connaissances informatiques approfondies. Les *)
  27. (* utilisateurs sont donc invités à charger et tester l'adéquation du *)
  28. (* logiciel à leurs besoins dans des conditions permettant d'assurer la *)
  29. (* sécurité de leurs systèmes et ou de leurs données et, plus généralement, *)
  30. (* à l'utiliser et l'exploiter dans les mêmes conditions de sécurité. *)
  31. (* *)
  32. (* Le fait que vous puissiez accéder à cet en-tête signifie que vous avez *)
  33. (* pris connaissance de la licence CeCILL, et que vous en avez accepté les *)
  34. (* termes. *)
  35. (******************************************************************************)
  36. open Core.Std;;
  37. open Command;;
  38. (* Module containing the definition of the interface of OcLaunch *)
  39. (* Type to return result of the work with common arguments *)
  40. type return_arg = {
  41. rc : Settings_t.rc_file;
  42. }
  43. (* A set of default arguments, usable with most of the commands *)
  44. let shared_params =
  45. let open Param in
  46. (* Way to treat common args *)
  47. return (fun verbosity no_color rc_file_name ->
  48. (* Set the level of verbosity *)
  49. Const.verbosity := verbosity;
  50. (* Do not use color *)
  51. Const.no_color := no_color;
  52. (* Use given rc file, should run the nth argument if present *)
  53. Const.rc_file := (Lazy.return rc_file_name);
  54. (* Debugging *)
  55. Messages.debug (sprintf "Verbosity set to %i" !Const.verbosity);
  56. Messages.debug (sprintf "Color %s" (match !Const.no_color with true -> "off" | false -> "on"));
  57. Messages.debug (sprintf "Configuration file is %s" (Lazy.force !Const.rc_file));
  58. Messages.debug (sprintf "Tmp file is %s" Const.tmp_file);
  59. (* Obtain data from rc_file *)
  60. let rc_content = File_com.init_rc () in
  61. { rc = rc_content } (* We use type for futur use *)
  62. )
  63. (* Flag to set verbosity level *)
  64. <*> flag "-v" (optional_with_default !Const.verbosity int)
  65. ~aliases:["--verbose" ; "-verbose"]
  66. ~doc:"[n] Set verbosity level. \
  67. The higher n is, the most verbose the program is."
  68. (* Flag to set colors *)
  69. <*> flag "--no-color" no_arg
  70. ~aliases:["-no-color"]
  71. ~doc:" Use this flag to disable color usage."
  72. (* Flag to use different rc file *)
  73. <*> flag "-c" (optional_with_default (Lazy.force !Const.rc_file) file)
  74. ~aliases:["--rc" ; "-rc"]
  75. ~doc:"file Read configuration from the given file and continue parsing."
  76. ;;
  77. (* basic-commands *)
  78. (* To reset tmp file *)
  79. let reset =
  80. basic
  81. ~summary:"OUTDATED\n Reinitialises launches of a given [command] to [n]. \
  82. If no [n] is given, the entry is deleted. \
  83. With neither [command] nor [n], all entries are reseted."
  84. Spec.(
  85. empty
  86. +> shared_params
  87. +> anon ("command_number" %: int)
  88. +> anon ("target_number" %: int)
  89. )
  90. (fun { rc } reset_cmd default_n () ->
  91. Tmp_file.reset ~rc reset_cmd default_n
  92. )
  93. ;;
  94. (* To list each commands with its number *)
  95. let list =
  96. basic
  97. ~summary:"Print a list of all commands with their number. Useful to launch with number. \
  98. Displays a star next to next command to launch."
  99. Spec.(
  100. empty
  101. +> shared_params
  102. )
  103. (fun { rc } () ->
  104. List_rc.run ~rc)
  105. ;;
  106. (* To add a command to rc file, from stdin or directly *)
  107. let add =
  108. basic
  109. ~summary:"Add the command given on stdin to the configuration file at a \
  110. given position ([NUMBER]). If nothing is given, append it."
  111. Spec.(
  112. empty
  113. +> shared_params
  114. +> anon (maybe ("number" %: int))
  115. )
  116. (fun { rc } num_cmd () ->
  117. Add_command.run ~rc num_cmd
  118. )
  119. ;;
  120. (* To remove a command from rc file *)
  121. let delete =
  122. basic
  123. ~summary:"Remove the [COMMAND_NUMBER]th command from configuration file. \
  124. If [COMMAND_NUMBER] is absent, remove last one."
  125. Spec.(
  126. empty
  127. +> shared_params
  128. +> anon (maybe ("command_number" %: int))
  129. )
  130. (fun { rc } num_cmd () ->
  131. (*Tmp_file.reset ~rc reset_cmd 0)*)
  132. Remove_command.run ~rc num_cmd)
  133. ;;
  134. (* To display current state *)
  135. let state =
  136. basic
  137. ~summary:"Display current state of the program."
  138. Spec.(
  139. empty
  140. +> shared_params
  141. )
  142. (fun _ () ->
  143. State.print_current ())
  144. ;;
  145. (* To edit the nth command *)
  146. let edit =
  147. basic
  148. ~summary:"Edit the [COMMAND_NUMBER]th command of the rc file in your \
  149. $EDITOR. May be used to add new entries."
  150. Spec.(
  151. empty
  152. +> shared_params
  153. +> anon ("command_number" %: int)
  154. )
  155. (fun { rc } default_n () ->
  156. Edit_command.run ~rc default_n)
  157. ;;
  158. (* To display informations about the licence *)
  159. let licence =
  160. basic
  161. ~summary:"Display the licence of the program"
  162. Spec.(
  163. empty
  164. +> shared_params
  165. +> flag "-header" no_arg
  166. ~doc:" Display the text of the licence"
  167. )
  168. (fun _ header () ->
  169. let cecill = not(header) in (* When cecill is false, it displays the header *)
  170. Licencing.print ~cecill
  171. )
  172. ;;
  173. (* Run th enth command, default use *)
  174. let default =
  175. basic
  176. ~summary:"Run the [COMMAND_NUMBER]th command"
  177. Spec.(
  178. empty
  179. +> shared_params
  180. +> anon (maybe ("command_number" %: int))
  181. )
  182. (fun { rc } n () ->
  183. Default.run ~rc n)
  184. let run ~version ~build_info () =
  185. let exit_code =
  186. match
  187. group
  188. ~summary:"OcLaunch program is published under CeCILL licence.\n You may \
  189. run the program with 'licence' command or see \
  190. http://cecill.info/licences/Licence_CeCILL_V2.1-en.html (http://huit.re/TmdOFmQT) for details."
  191. [ ( "reset-tmp", reset) ; ("list", list) ; ("add", add) ; ("delete",
  192. delete) ; ("state", state) ; ("edit", edit) ; ("licence", licence) ; ("run", default) ]
  193. ~readme:(fun () -> "Use '-h' flag to get help (it works both after the \
  194. name of the software and with a subcommand). For further help, see http://oclaunch.tuxfamily.org for help.")
  195. |> run ~version ~build_info
  196. with
  197. | () -> `Exit 0
  198. | exception message ->
  199. "Exception: " ^ (Exn.to_string message)
  200. |> Messages.warning;
  201. `Exit 20
  202. in
  203. (* Reset display *)
  204. Messages.reset ();
  205. exit_code
  206. ;;