LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-13-2019, 02:32 AM   #1
Suraj Patil
Member
 
Registered: Jun 2016
Posts: 78

Rep: Reputation: Disabled
click python example


Hello folk need help
using python click module only
=========================================
[python]
import click


@click.group(chain=True)
def cli():
pass


@cli.command('name')
@click.argument('name1', nargs=-1)
@click.option('--firstname', is_flag=True)
@click.option('--lastname', is_flag=True)
def name(name1, firstname, lastname):

# import pdb; pdb.set_trace()
first, last = name1.split()
if firstname:
click.echo(first)
elif lastname:
click.echo(last)


if __name__ == '__main__':
cli()

[\python]
==========================
expected output like
python foo.py name firstname lastname >> print firstname + lastname

python foo.py name firstname lastname --firstname >> it will print firstname

same for the lastname
 
Old 09-13-2019, 02:40 AM   #2
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.6
Posts: 3,786

Rep: Reputation: 1276Reputation: 1276Reputation: 1276Reputation: 1276Reputation: 1276Reputation: 1276Reputation: 1276Reputation: 1276Reputation: 1276
You need to edit your post and put the code in code tags to preserve the required spacing.
 
Old 09-13-2019, 08:07 AM   #3
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 21,952

Rep: Reputation: 5814Reputation: 5814Reputation: 5814Reputation: 5814Reputation: 5814Reputation: 5814Reputation: 5814Reputation: 5814Reputation: 5814Reputation: 5814Reputation: 5814
Quote:
Originally Posted by Suraj Patil View Post
Hello folk need help using python click module only
=========================================
[python]
import click


@click.group(chain=True)
def cli():
pass


@cli.command('name')
@click.argument('name1', nargs=-1)
@click.option('--firstname', is_flag=True)
@click.option('--lastname', is_flag=True)
def name(name1, firstname, lastname):

# import pdb; pdb.set_trace()
first, last = name1.split()
if firstname:
click.echo(first)
elif lastname:
click.echo(last)


if __name__ == '__main__':
cli()

[\python]
==========================

expected output like
python foo.py name firstname lastname >> print firstname + lastname
python foo.py name firstname lastname --firstname >> it will print firstname
same for the lastname
Ok...so as scasey said, you didn't post the indented code in CODE tags, and you (again) are just asking a question without providing details or showing your efforts. What version of Python? What are the ACTUAL ERRORS you're getting, that differ from what you want? Or are you just posting this and wanting us to do it for you?

You have been here for three years now; your last three posts, including this one, have shown no effort, nor have you even bothered to follow up:
https://www.linuxquestions.org/quest...st-4175660568/
https://www.linuxquestions.org/quest...on-4175660598/
 
Old 09-13-2019, 03:42 PM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,221
Blog Entries: 11

Rep: Reputation: 3172Reputation: 3172Reputation: 3172Reputation: 3172Reputation: 3172Reputation: 3172Reputation: 3172Reputation: 3172Reputation: 3172Reputation: 3172Reputation: 3172
As others have asked, to preserve formatting please place your code snippets inside [CODE]...[/CODE] tags. You may type those yourself or click the "#" button in the edit controls.

Proper indenting is required for python code to be readable by others.

Please also review the Site FAQ for guidance in posting your questions and general forum usage. Especially, read the link in that page, How To Ask Questions The Smart Way. The more effort you put into understanding your problem and framing your questions, the better others can help!
 
Old 09-15-2019, 11:56 AM   #5
rbtylee
Bodhi Developer
 
Registered: Jun 2018
Location: Ladson, SC US
Distribution: Bodhi
Posts: 222

Rep: Reputation: 227Reputation: 227Reputation: 227
Well aside from improper formatting of the OP and my own unfamiliarity with the click module, I think what the OP wants is something along the lines of:

Code:
import click

@click.group()
def cli():
	pass

@cli.command('name')
@click.argument('name1', nargs=-1)
@click.option('--firstname', is_flag=True)
@click.option('--lastname', is_flag=True)
def name(name1, firstname, lastname):

	first, last = name1[0],name1[-1]
	if firstname:
		click.echo(first)
	elif lastname:
		click.echo(last)
	elif (not firstname and not lastname):
		click.echo(first+' '+last)


if __name__ == '__main__':
	cli()
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Computer detecting right-click as left-click, left-click as left-click and middle with 2 fingers pressed as right-click Festerdam Linux - Newbie 5 06-19-2017 05:41 PM
I got error while installing python-tk python-psycopg2 python-twisted saili kadam Linux - Newbie 1 09-05-2015 03:03 AM
How to Rewrite www.example.com to example.com jayakumar01 Linux - Server 2 06-22-2012 09:28 AM
bind9: example.com works, www.example.com doesnt, and how to block urls Shwick Linux - Server 6 09-12-2011 12:26 AM
LXer: Python Python Python (aka Python 3) LXer Syndicated Linux News 0 08-05-2009 08:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:30 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration