Hack Forums
[Python] adding CLI function to my code. - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] adding CLI function to my code. (/showthread.php?tid=5901157)



adding CLI function to my code. - giwrgosev - Oct 29, 2018

Hey there,I'm new to programming. I'm working on this project and I want to add to it slash commands (e.g /help /show etc.).Any ideas on how to do it?Any help would be very appreciated.


RE: adding CLI function to my code. - Meedman - Oct 29, 2018

Look about raw_input() function I hope it will help you :)


RE: adding CLI function to my code. - giwrgosev - Oct 29, 2018

(Oct 29, 2018 01:19 PM)Meedman Wrote:  Look about raw_input() function I hope it will help you :)

I'll check it out later bud.Thanks for helping me.


RE: adding CLI function to my code. - Goodies - Oct 29, 2018

There's surprisingly no great utility for this. The go-to most people use is the "cmd" package. It's simple and it's built into the standard library. But it's not super intuitive or particularly well-documented. There are better tutorials on blogs and stuff than there are on the docs.python.org page.


RE: adding CLI function to my code. - Infamia - Oct 29, 2018

well there's either an object-oriented way to pursue it where you view everything like a parser and lexer would, parse it initially, tokenize it, separate operands, determine types, detect strings, etc.; a more than-usual advanced parser, but worth it in the end.

i have some code-based resources (which probably aren't useful if you're at the degree of python i assume you are), but good luck:

parsing strings
parsing general tokens
parsing octal/hex/denary-based integers


RE: adding CLI function to my code. - StreetSquirrel - Oct 29, 2018

I think I have an idea what you mean, check this out and tell me if it's what you were going for.

Code
# User input asking for a command
user = input('type in command > ')

#example of a help menu
help_menu = '''
example
help
menu

'''

#if the user types in /help then print out the help menu
if user == "/help":
print (help_menu)


I think this is what you're trying to do? Let me know if I'm mistaken


RE: adding CLI function to my code. - dv989 - Oct 30, 2018

argparse might help -- you can add something such as...

test.py -arg1 value -arg2 value

etc


RE: adding CLI function to my code. - giwrgosev - Oct 30, 2018

(Oct 29, 2018 03:25 PM)Goodies Wrote:  There's surprisingly no great utility for this. The go-to most people use is the "cmd" package. It's simple and it's built into the standard library. But it's not super intuitive or particularly well-documented. There are better tutorials on blogs and stuff than there are on the docs.python.org page.

thanks for your feedback bud.Will consider this.


(Oct 29, 2018 06:34 PM)Infamia Wrote:  well there's either an object-oriented way to pursue it where you view everything like a parser and lexer would, parse it initially, tokenize it, separate operands, determine types, detect strings, etc.; a more than-usual advanced parser, but worth it in the end.

i have some code-based resources (which probably aren't useful if you're at the degree of python i assume you are), but good luck:

parsing strings
parsing general tokens
parsing octal/hex/denary-based integers

thanks for your help.I believe argparse is what I waas looking for.


RE: adding CLI function to my code. - dontspamthisplease - Oct 30, 2018

(Oct 30, 2018 10:08 AM)dv989 Wrote:  argparse might help -- you can add something such as...

test.py -arg1 value -arg2 value

etc

Thank you mate, I almost lost all hope after reading someone say "there is no nice way" or something like that lmao

This is python, almost everything has a built-in way

User argparse and sys libs, get the args from sys.argv


RE: adding CLI function to my code. - Keat - Oct 30, 2018

If you are using python 2.x then cmd module is good. If you are on 3 then use cmd2.

I am actually in a huge project now that I am using cmd2 and argparser. It is very nice and works pretty dang good. If you run into any problems let me know.


RE: adding CLI function to my code. - giwrgosev - Oct 31, 2018

(Oct 30, 2018 06:41 PM)Keat Wrote:  If you are using python 2.x then cmd module is good. If you are on 3 then use cmd2.

I am actually in a huge project now that I am using cmd2 and argparser. It is very nice and works pretty dang good. If you run into any problems let me know.

ofc buddy,thanks for your help.


(Oct 30, 2018 01:13 PM)dontspamthisplease Wrote:  Thank you mate, I almost lost all hope after reading someone say "there is no nice way" or something like that lmao

This is python, almost everything has a built-in way

User argparse and sys libs, get the args from sys.argv

thats what i thought,thanks.