Ferreteria/v0.6/clade/IO/Aspect/Connx/run/Starter/aux/CmdLine

From Woozle Writes Code
< Ferreteria‎ | v0.6‎ | clade‎ | IO‎ | Aspect‎ | Connx‎ | run/Starter
Jump to navigation Jump to search
clade: IO\Aspect\Connx\run\Starter\aux\CmdLine
Clade Family
StandardBase CmdLine (none)
Clade Aliases
Alias Clade
Base* [c,i] Aux\StandardBase
Hints* [c,i] IO\Aspect\Connx\aux\Hints
QArr* [c,i] Data\Mem\QVar\Arr
QStr* [c,i] Data\Mem\QVar\Str
Parser* [c,i] Sys\Parsing\v2a\Chunker
SelfIface Sys\Data\Codec\aux\CmdLine
QStream* [c,i] IO\Aspect\Connx\aux\Q\Stream
Subpages

About

  • Purpose: This is kind of a specialty thing for handling command-lines as either array or string data.
    • Maybe there's a way to better integrate it into the Codec class-structure, but for now it's eluding me.
      • 2026-03-14 I think Codec should probably be its immediate parent, but the two APIs may be a little too different for this to work well.

Thinking

  • 2026-01-09
    • Up to now, the data could be stored in either array or string format, with the other format being calculated if needed. However...
    • Functionality to combine the contents of one CmdLine object with another (EmbedWithin()) means that we need to decide on a default storage format -- array or string. I think I'm going with array. For now, switching from string will only be forced if combination is needed.

History

  • 2026-01-05 Started.
  • 2026-03-14 moved from [WFe]Sys\Data\Codec\aux[WFe]IO\Aspect\Connx\Runner\aux

Code

as of 2026-03-14:

interface iCmdLine extends BaseIface {
    // SETUP
    static function FromVoid() : SelfIface;
    static function FromString(string $s) : SelfIface;
    static function FromArray(array $ar) : SelfIface;
    // ACCESS: read
    function AsString() : string;
    function AsArray() : array;
    // ACCESS: write
    function WithArray(array $a);
    function EmbedWithin(SelfIface $oCmd);
    // MODE
    function DoWait(?bool $b=NULL) : bool;
    // OBJECTS
    function Hints() : HintsIface;
    function QOLecturer() : QStreamIface;
    function QOListener() : QStreamIface;
}
class cCmdLine extends BaseClass implements SelfIface {

    // ++ SETUP ++ //

    public static function FromVoid() : SelfIface { return new static; }

    public static function FromString(string $s) : SelfIface {
        $oThis = new static;
        $oThis->QString()->SetIt($s);
        return $oThis;
    }
    public static function FromArray(array $a) : SelfIface {
        $oThis = new static;
        $oThis->WithArray($a);
        return $oThis;
    }

    // -- SETUP -- //
    // ++ ACCESS: read ++ //

    public function Spawn() : SelfIface {
        $oNew = new static;
        $oNew->qsCmd = QStrClass::FromSame($this->QString());
        $oNew->qaCmd = QArrClass::FromSame($this->QArray());
        return $oNew;
    }

    public function AsString() : string {
        return $this->QString()->HasIt() ? $this->QString()->GetIt() : $this->ToString();
    }
    public function AsArray() : array {
        return $this->QArray()->HasIt() ? $this->QArray()->GetIt() : $this->ToArray();
    }

    // ++ ACCESS: display ++ //

    public function VIEW_Inline() : string {
        $q = $this->QString();
        $sStr = $q->HasIt() ? ('string=['.$q->GetIt().']') : '[string n/a]';
        $q = $this->QArray();
        $sArr = $q->HasIt() ? ('array N='.count($q->GetIt())) : '[array n/a]';
        $sOID = $this->ObjectID();
        return "(oid$sOID) $sStr / $sArr";
    }

    // ++ ACCESS: write ++ //

    // ACTION: Embed the current CmdLine within a longer one, consisting of a prefix ($oPfx) and optional suffix ($oSfx).
    // RULE: Array elements are either strings (I think) or CmdLine objects.
    public function EmbedWithin(SelfIface $oPfx, ?SelfIface $oSfx=NULL) {
        $arPfx = $oPfx->AsArray();
        $oOld = $this->Spawn();    // copy old contents to embed
        #$this->AmHereShort('PFX: '.$oPfx->VIEW_Inline().' / THIS: '.$this->VIEW_Inline().' / OLD: '.$oOld->VIEW_Inline());
        if (is_object($oSfx)) {
            $arSfx = $oSfx->AsArray();
            $ar = array_merge($arPfx,[$oOld],$arSfx);
        } else {
            $ar = array_merge($arPfx,[$oOld]);
        }
        $this->QArray()->SetIt($ar);
        $this->QString()->ZapIt();
    }

    // ++ MODE ++ //

    // PURPOSE: signals whether the Runner code should wait for the process to end (buffering received data) or return right after executing
    private $bWait = TRUE;  // default
    public function DoWait(?bool $b=NULL) : bool { return is_bool($b) ? ($this->bWait = $b) : $this->bWait; }

    // -- MODE -- //
    // ++ ACCESS ++ //

    public function WithArray(array $a) { $this->QArray()->SetIt($a); $this->QString()->ZapIt(); }
    public function WithString(string $s) { $this->QString()->SetIt($s); $this->QArray()->ZapIt(); }

    // ++ ACCESS (internal): storage ++ //

    private $qsCmd=NULL;
    protected function QString() : QStrIface { return $this->qsCmd ?? ($this->qsCmd = QStrClass::AsNew()); }
    private $qaCmd=NULL;
    protected function QArray() : QArrIface { return $this->qaCmd ?? ($this->qaCmd = QArrClass::AsNew()); }

    // ++ ACCESS (internal): conversion ++ //

    // PURPOSE: Command is stored as an array -- convert to string.
    protected function ToString() : string {
        $ar = $this->QArray()->GetIt();
        $s = NULL;
        foreach ($ar as $vPiece) {
            if (is_string($s)) $s .= ' ';
            if (is_string($vPiece)) {
                $s .= $vPiece;
            } else {
                $s .= escapeshellarg($vPiece->AsString());
            }
        }
        return $s;
    }
    // PURPOSE: Command is stored as a string -- convert to array.
    protected function ToArray() : array {
        $s = $this->QString()->GetIt();
        $oParser = new ParserClass($s);
        $oGene = $oParser->Parse();
        return $oGene->AsStringArray();
    }

    // -- ACCESS -- //
    // ++ OBJECTS ++ //

    private $oaHints = NULL;
    public function Hints() : HintsIface { return $this->oaHints ?? ($this->oaHints = new HintsClass); }

    private $qoLect = NULL;
    public function QOLecturer() : QStreamIface { return $this->qoLect ?? ($this->qoLect = QStreamClass::AsNew()); }
    private $qoLstn = NULL;
    public function QOListener() : QStreamIface { return $this->qoLstn ?? ($this->qoLstn = QStreamClass::AsNew()); }
    private $qoErrs = NULL;
    public function QOErrorBin() : QStreamIface { return $this->qoErrs ?? ($this->qoErrs = QStreamClass::AsNew()); }

    // -- OBJECTS -- //
}