<commit_before><?php
/*
 * This file is part of the RBot app.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace RBot;

use GetOptionKit\Option;
use GetOptionKit\OptionCollection;
use GetOptionKit\OptionParser;

use GetOptionKit\Exception\InvalidOptionException;
use GetOptionKit\Exception\RequireValueException;
use GetOptionKit\Exception\NonNumericException;

use Exception;

/**
 * Base class for App Commands
 */
abstract class Command
{

    /**
     * Commands spec
     * @var array
     */
    protected $_options;

    /**
     * Command spec result
     * @var [type]
     */
    protected $_result;

    /**
     * No result
     * @var boolean
     */
    protected $_no_result = true;

    /**
     * Construct
     */
    public function __construct()
    {
        $this->_options = new OptionCollection;
        $this->setOptions();
    }

    /**
     * Child class methods
     */
    public function setOptions(){}
    public function preProcess() {}
    public function process(){}

    /**
     * Run the command
     *
     * 1. Call pre specs process
     * 2. Parse argv
     * 2. Call specs option methods if exists (syntax: opt_[spec-key-name])
     * 3. Call main process method
     */
    public function run()
    {
        $this->preProcess();
        $this->_parseArgv();

        $this->_no_result = true;
        foreach($this->_options as $k => $s) {
            if($this->_result->has($k)) {
                $this->_no_result = false;
                if(method_exists($this, 'opt_'.$k)) {
                    $m = 'opt_'.$k;
                    $this->$m($s->getValue());
                }
            }
        }

        //print_r($this->_result);
        
        $this->process();
    }

    /**
     * Show help
     */
    public function help()
    {
        $max_length = 0;
        foreach($this->_options as $i => $o) {
            $opt_line = $this->_renderOption($o);
            $length = strlen($opt_line);
            if($length > $max_length) {
                $max_length = $length;
            }
        }

        foreach($this->_options as $o) {
            $opt_line = $this->_renderOption($o);
            $length = strlen($opt_line);

            for($i=1;$i <= (($max_length-$length)+8);++$i) {
                $opt_line .= ' ';
            }

            Console::add($opt_line.' '.$o->desc);
        }

        Console::add('');
        Console::outputDie();
    }

    /**
     * Format option to a string
     * 
     * @param  object $opt GetOptionKit\Option
     * @return string
     */
    public function _renderOption($opt)
    {
        $c1 = '';
        if ( $opt->short && $opt->long ) {
            $c1 = sprintf('-%s, --%s',$opt->short,$opt->long);
        } elseif( $opt->short ) {
            $c1 = sprintf('-%s',$opt->short);
        } elseif( $opt->long ) {
            $c1 = sprintf('--%s',$opt->long );
        }
        if(!defined('RBOT_CLI')) {
            $c1 .= str_replace(['<','>'], ['(',')'], strtolower($opt->renderValueHint()));
        }
        else $c1 .= $opt->renderValueHint();
        return trim($c1);
    }

    /**
     * __toString()<commit_msg>command help output are no more logged<commit_after><?php
/*
 * This file is part of the RBot app.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace RBot;

use GetOptionKit\Option;
use GetOptionKit\OptionCollection;
use GetOptionKit\OptionParser;

use GetOptionKit\Exception\InvalidOptionException;
use GetOptionKit\Exception\RequireValueException;
use GetOptionKit\Exception\NonNumericException;

use Exception;

/**
 * Base class for App Commands
 */
abstract class Command
{

    /**
     * Commands spec
     * @var array
     */
    protected $_options;

    /**
     * Command spec result
     * @var [type]
     */
    protected $_result;

    /**
     * No result
     * @var boolean
     */
    protected $_no_result = true;

    /**
     * Construct
     */
    public function __construct()
    {
        $this->_options = new OptionCollection;
        $this->setOptions();
    }

    /**
     * Child class methods
     */
    public function setOptions(){}
    public function preProcess() {}
    public function process(){}

    /**
     * Run the command
     *
     * 1. Call pre specs process
     * 2. Parse argv
     * 2. Call specs option methods if exists (syntax: opt_[spec-key-name])
     * 3. Call main process method
     */
    public function run()
    {
        $this->preProcess();
        $this->_parseArgv();

        $this->_no_result = true;
        foreach($this->_options as $k => $s) {
            if($this->_result->has($k)) {
                $this->_no_result = false;
                if(method_exists($this, 'opt_'.$k)) {
                    $m = 'opt_'.$k;
                    $this->$m($s->getValue());
                }
            }
        }

        //print_r($this->_result);
        
        $this->process();
    }

    /**
     * Show help
     */
    public function help()
    {
        $max_length = 0;
        foreach($this->_options as $i => $o) {
            $opt_line = $this->_renderOption($o);
            $length = strlen($opt_line);
            if($length > $max_length) {
                $max_length = $length;
            }
        }

        foreach($this->_options as $o) {
            $opt_line = $this->_renderOption($o);
            $length = strlen($opt_line);

            for($i=1;$i <= (($max_length-$length)+8);++$i) {
                $opt_line .= ' ';
            }

            Console::add($opt_line.' '.$o->desc);
        }

        Console::noLog();
        Console::add('');
        Console::outputDie();
    }

    /**
     * Format option to a string
     * 
     * @param  object $opt GetOptionKit\Option
     * @return string
     */
    public function _renderOption($opt)
    {
        $c1 = '';
        if ( $opt->short && $opt->long ) {
            $c1 = sprintf('-%s, --%s',$opt->short,$opt->long);
        } elseif( $opt->short ) {
            $c1 = sprintf('-%s',$opt->short);
        } elseif( $opt->long ) {
            $c1 = sprintf('--%s',$opt->long );
        }
        if(!defined('RBOT_CLI')) {
            $c1 .= str_replace(['<','>'], ['(',')'], strtolower($opt->renderValueHint()));
        }
        else $c1 .= $opt->renderValueHint();
        return trim($c1);
    }

    /**
     * __toString()
{'fraction_non_alphanumeric': 0.15930144267274107, 'fraction_numerical': 0.00425208807896735, 'mean_word_length': 1.7396006655574043, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1825783', 'n_tokens_mistral': 2012, 'n_tokens_neox': 1965, 'n_words': 432}