DomenicF Posted September 4, 2012 Share Posted September 4, 2012 Hello, I have a script that takes two parameters that I retrieve with $argv. But, I see a lot of scripts in other languages that have "--help" and "-h" in them and other options/flags like that, and I'm curious to find out the correct way to add these to my script. Any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/267974-command-line-help/ Share on other sites More sharing options...
requinix Posted September 4, 2012 Share Posted September 4, 2012 I've written a couple command-line-parsing functions, none of which I have on hand to demonstrate. If you want to write one it depends how much support you need from it (because there are a few possible styles to choose from - I want to say GNU, POSIX, and BSD?). At a minimum all you need is if (in_array("-h", $argv) || in_array("--help", $argv)) { Quote Link to comment https://forums.phpfreaks.com/topic/267974-command-line-help/#findComment-1375052 Share on other sites More sharing options...
DomenicF Posted September 4, 2012 Author Share Posted September 4, 2012 Thanks for your response. I have this code right now, but nothing has changed with the output. if (!in_array("-h", $argv) || !in_array("--help", $argv)) { // stuff here } else { echo "Help!"; } Quote Link to comment https://forums.phpfreaks.com/topic/267974-command-line-help/#findComment-1375054 Share on other sites More sharing options...
trq Posted September 4, 2012 Share Posted September 4, 2012 Take a look at getopt, no point in rolling your own. Quote Link to comment https://forums.phpfreaks.com/topic/267974-command-line-help/#findComment-1375063 Share on other sites More sharing options...
DomenicF Posted September 4, 2012 Author Share Posted September 4, 2012 Take a look at getopt, no point in rolling your own. I took a look into that, and tried putting it into practice, but could not figure it out. The examples they give aren't very helpful to be honest, would you mind writing an example for me? I would really appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/267974-command-line-help/#findComment-1375066 Share on other sites More sharing options...
requinix Posted September 4, 2012 Share Posted September 4, 2012 Take a look at getopt, no point in rolling your own. Ooh, they greatly improved it in 5.3. I'll have to check it out again. Quote Link to comment https://forums.phpfreaks.com/topic/267974-command-line-help/#findComment-1375067 Share on other sites More sharing options...
trq Posted September 4, 2012 Share Posted September 4, 2012 The examples they give aren't very helpful to be honest, would you mind writing an example for me? I would really appreciate it. I'm not sure I could explain it any simpler than the manual. Where exactly are you stuck? Quote Link to comment https://forums.phpfreaks.com/topic/267974-command-line-help/#findComment-1375118 Share on other sites More sharing options...
DomenicF Posted September 4, 2012 Author Share Posted September 4, 2012 You're right, I just wasn't fully understanding the function before. I tried a few things and got it working. Appreciate the link! Quote Link to comment https://forums.phpfreaks.com/topic/267974-command-line-help/#findComment-1375230 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.