Welcome Guest, Not a member yet? Register   Sign In
CLI Alternate Input Method Question
#1

[eluser]Ashes-to-Ashes[/eluser]
Using CI 2.0 and a command line flag function, when it command line I want to stay in CI instead of passing back to the command line. So, when the screen is done displaying, it can ask for input. I could do this with BASH script, or perhaps another PHP script calling CI but that seems weird. It always displays the output after the readline, is their a place to put code that will execute after the output?
#2

[eluser]Basketcasesoftware[/eluser]
CI is a fancy print command but it has no command line options. Your server gets a browser request, it calls the index.php file at the root, generates a web page based on the instructions, then exits. What you are asking is if there was a way to stay in a web page after it's finished rendering. Nope.

How the heck are you trying to "run" it? executing index.php from the command line? CI is not a stand alone program.
#3

[eluser]Ashes-to-Ashes[/eluser]
I am running it with php-cli package on ubuntu, which let's me type 'php index.php controller function paramater...' And it will run perfectly. I can put a command line check by seeing if one of the server array keys is set, and adjust my output to HTML or Command Line output based on that.

So, yes I do want to stay in the program after rendering. But in this case it would not be running as a web page.

I have thought of two ways so far, but want to be careful how I implement them, although I am using Mercurial so I can roll back.

Option 1:
I think, that I can over ride the output class, so that after it has rendered the page it will check to see if it is in command line mode, and if it should exit or ask for input.

Option 2: Run CodeIgniter from a wrapper php script designed for the command line. i.e:

php wrapper

wrapper would then exec using passthru("php index.php $controller $function $params");
Then, by setting command line labels in my output, I could use readline in the wrapper, which would then reload the index file again.

Option 3: Is really easy. Use w3m to access local host, BUT this requires that the domain is setup, and that a web server is running, where as otherwise I can access non web directories containing CI code and run it through ssh (my preference)
#4

[eluser]Basketcasesoftware[/eluser]
A wrapper is what you need then. In fact that is exactly what a browser is but, as you point out for option three is limiting (and overkill). The only use I can see for this is selecting the execution of different controllers and their methods. You'd have to create some kind of input function. Of course you can use a controller for that Smile. But if you want to use any forms or Java? forget about it unless you create one hell of a wrapper. If you have a browser program that can take a html file as a command line input then you could just pass the output of your CodeIgniter program to it somehow and wouldn't have to worry about running a full server. Personally I think this is more trouble than it's worth.
#5

[eluser]Basketcasesoftware[/eluser]
A wrapper is what you need then. In fact that is exactly what a browser is but, as you point out for option three is limiting (and overkill). The only use I can see for this is selecting the execution of different controllers and their methods. You'd have to create some kind of input function. Of course you can use a controller for that Smile. But if you want to use any forms or Java? forget about it unless you create one hell of a wrapper. If you have a browser program that can take a html file as a command line input then you could just pass the output of your CodeIgniter program to it somehow and wouldn't have to worry about running a full server. Personally I think this is more trouble than it's worth.
#6

[eluser]Basketcasesoftware[/eluser]
I had a weird problem that caused that double post. I don't seem to be able to delete my own post even though the button is showing up for me. Maybe one of the moderators will actually see and delete it. The extra that is.
#7

[eluser]Ashes-to-Ashes[/eluser]
[quote author="Basketcasesoftware" date="1298347505"]A wrapper is what you need then. In fact that is exactly what a browser is but, as you point out for option three is limiting (and overkill). The only use I can see for this is selecting the execution of different controllers and their methods. You'd have to create some kind of input function. Of course you can use a controller for that Smile. But if you want to use any forms or Java? forget about it unless you create one hell of a wrapper. If you have a browser program that can take a html file as a command line input then you could just pass the output of your CodeIgniter program to it somehow and wouldn't have to worry about running a full server. Personally I think this is more trouble than it's worth.[/quote]

Well, it may be. Smile More trouble than it's worth that is. I probably will attempt to prove at least the theory just out of curiosity, but I might do it on a clone of my repository. If I figure out any examples of simple code I will post them for someone who may need this stuff more than me (in my case, I want it... but probably don't need it hehehe(

Ash
#8

[eluser]Ashes-to-Ashes[/eluser]
Code:
<?php                                                
chdir ('/varwww/');  
passthru ('php index.php');
// can switch to ncurses for a getkey type response ending on tab
readline ('Enter \' and then unique letter combination from field name or link then enter');

By putting the above in a loop, and building my CodeIgniter application so that when it builds links it stores an array of links and fields on the page, and accepts a parameter to the function for each link to be navigated to, or field to be edited, I should eventually be able to handle forms and anchor(link) navigation in a rudimentary manner. (I.E. no actual movement onto predisplayed form/ page, but by using the keyboard short cuts of Firefox I can actually simulate a find text "/" so find link "'" command and then read a tab key if it is text search "/" to simulate tabbing to the field after the label. and enter to end the input if it is a link search "'"
A blank enter could submit a form with all the fields set (by individual calls to the controller/form_function/field_name/field_value/field_name....

I just need a way to return the values of the form with any preset fields back to the calling script.

The nice thing, is that you should be able to run a cron job to add something using an entry form designed for human web usage. (such as add a log entry or update user list... I don't know... lol)

I should be able to do the same with session information by dumping it to a file or database that can be read by the calling script... but one thing at a time hehehe INPUT is the focus if this small experiment.

Ash
#9

[eluser]Ashes-to-Ashes[/eluser]
Well, here is my final attempt for today, which seems to answer my original question in one manner. I will experiment with over riding output class at some other point, and probably create a separate topic for it. Also on the to do list will be session replacement for command line usage... also new topic...

Thanks for being even slightly interested in my weird little experiment.

-- Ash

Code:
#!/usr/bin/php -q
<?php
chdir ('/var/www/');
ob_start();
passthru('php index.php', $result); // result is exit code
$content_grabbed=ob_get_contents();
ob_end_clean();

// http://invisible-island.net/ncurses/ncurses.faq.html#setvbuf_once
// ncurses causes problems withoutput buffer
// ncurses_init();
// could parse here and then output functions could all be pure code igniter functions
// this would be an alterative to writing output wrappers, which MAY speed up use when called through web browser
passthru ('clear');
//takes out new lines!!
//echo filter_var($content_grabbed, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
$output = $content_grabbed;
$output = preg_replace("'<style[^>]*>.*</style>'siU",'',$output); // remove css
$output = preg_replace("'<a[^>]*.*>'siU",'LINK: ',$output); // mark links

//ncurses_clear();
echo strip_tags($output); // remove other html
// NOTE change to ncurses based get key until tab key pressed to simulate browser action
readline ('Enter \' and then unique letter combination from field name or link then enter');
#10

[eluser]Phil Sturgeon[/eluser]
No wrapper is required, PHP can do this out of the box. The readline() extension makes it a bit more bash with line completion and line history, but is not required at all.

If you want to make interactive CLI programs with CI easily then look at my CLI library.

Just make a controller then use the $this->read() method to ask for a response.




Theme © iAndrew 2016 - Forum software by © MyBB