Sorry for the meaningless title. In the terminology of Cmdliner, a flag is an optional argument without a value, hence the meaningless of the title, but I did not found a better one.
I wanted to writte a little program that could be used like this :
ompdc playback pause
ompdc playback prev
ompdc playback stop
I had been able to make it work using Cmdliner.Arg.vflags_all
like in this part of code :
type playback_cmds = Play | Next | Prev | Pause | Stop
let playback_cmds_to_string = function
| Next -> "next"
| Pause -> "pause"
| Play -> "play"
| Prev -> "prev"
| Stop -> "stop"
let initialize_client {host; port} =
let connection = Mpd.Connection.initialize host port in
let client = Mpd.Client.initialize connection in
let _ = print_endline ("Mpd server : " ^ (Mpd.Client.mpd_banner client)) in
client
let playback common_opts cmd =
let {host; port} = common_opts in
let client = initialize_client {host; port} in
let cmd_str = playback_cmds_to_string cmd in
let _ = match cmd with
| Next -> ignore (Mpd.Playback.next client)
| Pause -> ()
| Play -> ()
| Prev -> ignore (Mpd.Playback.prev client)
| Stop -> ignore (Mpd.Playback.stop client)
in
let message = Printf.sprintf "%s:%d %s" host port cmd_str in
print_endline message
let playback_action =
let doc = "Play next song." in
let next = Next, Arg.info ["next"] ~doc in
let doc = "Toggle Play/Stop." in
let pause = Pause, Arg.info ["pause"] ~doc in
let doc = "Play the current song in the Mpd queue." in
let play = Play, Arg.info ["play"] ~doc in
let doc = "Stop playing songs." in
let stop = Stop, Arg.info ["stop"] ~doc in
let doc = "Play previous song." in
let prev = Prev, Arg.info ["prev"] ~doc in
Arg.(last & vflag_all [Pause] [next; pause; play; prev; stop])
let playback_t =
let doc = "Playback commands"
in
let man = [
`S Manpage.s_description;
`P "Playback commands for the current playlist (queue).";
`Blocks help_section; ]
in
Term.(const playback $ common_opts_t $ playback_action),
Term.info "playback" ~doc ~sdocs ~exits ~man
Now as you can see in the code, it remains two arguments that I have not implemented because I need to link a value/argument to these flags like this :
ompdc playback --pause
ompdc playback --prev
ompdc playback --stop
ompdc playback --play 1
opmdc playback --pause false
My problem is that all the commands in the playback Cmdline.term (play, prev, stop, play …) should be implemented with a flags because it force to choose a least the last one command :
for example :
ompdc playback --stop --next
will choose the last one because I used Arg.(last & vflag_all
but if I choose to implement the play command as a Arg.opt
which is an optionnal argument with a value, I am not sure that :
ompdc playback --stop --next
will not try to launch the two commands.
I hope I am clear enough, this is not easy to explain.