wca 0.46.0

The tool to make CLI ( commands user interface ). It is able to aggregate external binary applications, as well as functions, which are written in your language.
use super :: *;
use the_module ::
{
  parser ::Parser,
  VerifiedCommand,
  executor ::Context,
  Type,
  grammar ::Dictionary,
  verifier ::Verifier,

  Executor,
  // wtools
};

//

tests_impls! {
  fn basic()
  {
  // init parser
  let parser = Parser;

  // init converter
  let dictionary = &Dictionary ::former()
  .command
  (
   wca ::grammar ::Command ::former()
   .hint( "hint" )
   .long_hint( "long_hint" )
   .phrase( "command" )
   .routine( || println!( "hello" ) )
   .form()
 )
  .form();
  let verifier = Verifier;

  // init executor
  let raw_command = parser.parse( [ ".command" ] ).unwrap().commands.remove( 0 );
  let grammar_command = verifier.to_command( dictionary, raw_command ).unwrap();
  let executor = Executor ::former().form();

  // execute the command
  a_true!( executor.command( dictionary, grammar_command ).is_ok() );
 }

  fn with_subject()
  {
  // init parser
  let parser = Parser;

  // init converter
  let dictionary = &Dictionary ::former()
  .command
  (
   wca ::grammar ::Command ::former()
   .hint( "hint" )
   .long_hint( "long_hint" )
   .phrase( "command" )
   .subject().hint( "hint" ).kind( Type ::String ).optional( false ).end()
   .routine( | o: VerifiedCommand | o.args.get( 0 ).map( | a | println!( "{a:?}" ) ).ok_or_else( || "Subject not found" ) )
   .form()
 )
  .form();
  let verifier = Verifier;

  // init executor
  let executor = Executor ::former().form();

  // with subject
  let raw_command = parser.parse( [ ".command", "subject" ] ).unwrap().commands.remove( 0 );
  let grammar_command = verifier.to_command( dictionary, raw_command ).unwrap();

  // execute the command
  a_true!( executor.command( dictionary, grammar_command ).is_ok() );

  // without subject
  let raw_command = parser.parse( [ ".command" ] ).unwrap().commands.remove( 0 );
  let grammar_command = verifier.to_command( dictionary, raw_command );
  a_true!( grammar_command.is_err() );
 }

  fn with_property()
  {
  // init parser
  let parser = Parser;

  // init converter
  let dictionary = &Dictionary ::former()
  .command
  (
   the_module ::grammar ::Command ::former()
   .hint( "hint" )
   .long_hint( "long_hint" )
   .phrase( "command" )
   .subject().hint( "Any string." ).kind( Type ::String ).optional( true ).end()
   .routine( | o: VerifiedCommand | println!( "command executed" ) )
   .form()
 )
  .form();
  let verifier = Verifier;

  // init executor
  let executor = Executor ::former().form();

  // with subject
  let raw_command = parser.parse( [ ".command", "prop: value" ] ).unwrap().commands.remove( 0 );
  let grammar_command = verifier.to_command( dictionary, raw_command ).unwrap();

  // execute the command
  a_true!( executor.command( dictionary, grammar_command ).is_ok() );

  // with subject
  let raw_command = parser.parse( [ ".command", "subject" ] ).unwrap().commands.remove( 0 );
  let grammar_command = verifier.to_command( dictionary, raw_command );
  a_true!( grammar_command.is_ok() );

  // without subject (should still work as subject is optional)
  let raw_command = parser.parse( [ ".command" ] ).unwrap().commands.remove( 0 );
  let grammar_command = verifier.to_command( dictionary, raw_command );
  a_true!( grammar_command.is_ok() );
 }

  fn with_context()
  {
  use std ::sync :: { Arc, Mutex };

  // init parser
  let parser = Parser;

  // init converter
  let dictionary = &Dictionary ::former()
  .command
  (
   wca ::grammar ::Command ::former()
   .hint( "hint" )
   .long_hint( "long_hint" )
   .phrase( "check" )
   .routine
   (
  | ctx: Context |
  ctx
  .get()
  .ok_or_else( || "Have no value" )
  .and_then( | x: Arc< Mutex< i32 > > | if *x.lock().unwrap() != 1 { Err( "x not eq 1" ) } else { Ok( () ) } )
 )
   .form()
 )
  .form();
  let verifier = Verifier;
  let mut ctx = wca ::executor ::Context ::new( Mutex ::new( 1 ) );
  // init executor
  let executor = Executor ::former()
  .context( ctx )
  .form();

  let raw_command = parser.parse( [ ".check" ] ).unwrap().commands.remove( 0 );
  let grammar_command = verifier.to_command( dictionary, raw_command ).unwrap();

  // execute the command
  a_true!( executor.command( dictionary, grammar_command ).is_ok() );
 }

  fn without_routine()
  {
  // init parser
  let parser = Parser;

  // init converter
  let dictionary = &Dictionary ::former()
  .command
  (
   wca ::grammar ::Command ::former()
   .hint( "hint" )
   .long_hint( "long_hint" )
   .phrase( "command" )
   .form()
 )
  .form();
  let verifier = Verifier;

  // init executor
  let executor = Executor ::former().form();

  let raw_command = parser.parse( [ ".command" ] ).unwrap().commands.remove( 0 );
  let grammar_command = verifier.to_command( dictionary, raw_command ).unwrap();

  a_true!( executor.command( dictionary, grammar_command ).is_err() );
 }
}

//

tests_index! {
  basic,
  with_subject,
  with_property,
  with_context,
  without_routine,
}