Skip to content

Commit cdd6834

Browse files
committed
CLI commands support added
1 parent 06904d2 commit cdd6834

File tree

3 files changed

+73
-21
lines changed

3 files changed

+73
-21
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ You can download the latest version from the GitHub's [releases tab](https://git
3030
* Install the requirements: `pip install -r requirements.txt`
3131
* And you should be able to launch the project: `python -m kroger_cli`
3232

33+
CLI Commands
34+
------------
35+
36+
The application support non-interactive mode by passing a name of a command. An example on how to complete Kroger's Survey: `kroger-cli survey`.
37+
38+
Please use `kroger-cli --help` to see list of all available commands. Alternatively you can run the application without any command to launch the interactive mode (you can see the screenshot of it below).
39+
3340
Screenshots
3441
-----------
3542

@@ -60,6 +67,5 @@ The initial plan was to use plain HTTP (and `requests` package), however I could
6067

6168
### TODO
6269

63-
* Command Line Arguments, to allow something like that: `kroger-cli --clip-digital-coupons`
6470
* Purchased items (receipt data) Excel export, which could be useful for budgeting/categorization/filtering
6571
* Notification on when certain items go on sale

kroger_cli/__main__.py

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,51 @@
1+
import click
12
from kroger_cli.cli import KrogerCLI
23

4+
kroger_cli = KrogerCLI()
5+
6+
7+
@click.group(invoke_without_command=True)
8+
@click.pass_context
9+
@click.option('--disable-headless', is_flag=True, help='Disable chromium\'s headless mode (useful for debug).')
10+
def cli(ctx, disable_headless):
11+
if disable_headless:
12+
kroger_cli.api.browser_options['headless'] = False
13+
14+
# CLI call without a command
15+
if ctx.invoked_subcommand is None:
16+
kroger_cli.prompt_options()
17+
18+
19+
@click.command('account-info', help='Display account info.')
20+
def account_info():
21+
kroger_cli.option_account_info()
22+
23+
24+
@click.command('clip-coupons', help='Clip all digital coupons.')
25+
def clip_coupons():
26+
kroger_cli.option_clip_coupons()
27+
28+
29+
@click.command('purchases-summary', help='Purchases Summary.')
30+
def purchases_summary():
31+
kroger_cli.option_purchases_summary()
32+
33+
34+
@click.command('points-balance', help='Retrieve Points Balance.')
35+
def points_balance():
36+
kroger_cli.option_points_balance()
37+
38+
39+
@click.command('survey', help='Complete Kroger’s Survey (to earn 50 points).')
40+
def survey():
41+
kroger_cli.option_survey()
42+
43+
344
if __name__ == '__main__':
4-
cli = KrogerCLI()
5-
cli.prompt_options()
45+
cli.add_command(account_info)
46+
cli.add_command(clip_coupons)
47+
cli.add_command(purchases_summary)
48+
cli.add_command(points_balance)
49+
cli.add_command(survey)
50+
51+
cli()

kroger_cli/cli.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,6 @@ def __init__(self, config_file='config.ini'):
2424
self.init()
2525

2626
def init(self):
27-
if self.config['profile']['first_name'] != '':
28-
self.console.print(Panel('[bold]Welcome Back, ' + self.config['profile']['first_name'] + '! :smiley:\n'
29-
'[dark_blue]Kroger[/dark_blue] CLI[/bold]', box=box.ASCII))
30-
else:
31-
self.console.print(Panel('[bold]Welcome to [dark_blue]Kroger[/dark_blue] CLI[/bold] (unofficial command '
32-
'line interface)', box=box.ASCII))
33-
3427
self.prompt_store_selection()
3528

3629
if self.username is None and self.config['main']['username'] != '':
@@ -65,6 +58,13 @@ def prompt_credentials(self):
6558
self._set_credentials(username, password)
6659

6760
def prompt_options(self):
61+
if self.config['profile']['first_name'] != '':
62+
self.console.print(Panel('[bold]Welcome Back, ' + self.config['profile']['first_name'] + '! :smiley:\n'
63+
'[dark_blue]Kroger[/dark_blue] CLI[/bold]', box=box.ASCII))
64+
else:
65+
self.console.print(Panel('[bold]Welcome to [dark_blue]Kroger[/dark_blue] CLI[/bold] (unofficial command '
66+
'line interface)', box=box.ASCII))
67+
6868
while True:
6969
self.console.print('[bold]1[/bold] - Display account info')
7070
self.console.print('[bold]2[/bold] - Clip all digital coupons')
@@ -77,15 +77,15 @@ def prompt_options(self):
7777
self.console.rule()
7878

7979
if option == 1:
80-
self._option_account_info()
80+
self.option_account_info()
8181
elif option == 2:
82-
self._option_clip_coupons()
82+
self.option_clip_coupons()
8383
elif option == 3:
84-
self._option_purchases_summary()
84+
self.option_purchases_summary()
8585
elif option == 4:
86-
self._option_points_balance()
86+
self.option_points_balance()
8787
elif option == 5:
88-
self._option_survey()
88+
self.option_survey()
8989
elif option == 8:
9090
self.prompt_credentials()
9191
elif option == 9:
@@ -118,7 +118,7 @@ def _get_details_for_survey(self):
118118
if self.config['profile']['first_name'] == '':
119119
self.console.print('[bold]We need to retrieve the account info in order to fill out the survey form. '
120120
'Please wait..[/bold]')
121-
self._option_account_info()
121+
self.option_account_info()
122122

123123
greetings = False
124124
for field in helper.survey_mandatory_fields:
@@ -133,7 +133,7 @@ def _get_details_for_survey(self):
133133
self.config['profile'][field] = str(inp)
134134
self._write_config_file()
135135

136-
def _option_survey(self):
136+
def option_survey(self):
137137
self._get_details_for_survey()
138138

139139
result = self.api.complete_survey()
@@ -142,7 +142,7 @@ def _option_survey(self):
142142
else:
143143
self.console.print('[bold red]Couldn\'t complete the feedback form :([/bold red]')
144144

145-
def _option_account_info(self):
145+
def option_account_info(self):
146146
info = self.api.get_account_info()
147147
if info is None:
148148
self.console.print('[bold red]Couldn\'t retrieve the account info.[/bold red]')
@@ -151,7 +151,7 @@ def _option_account_info(self):
151151
self._write_config_file()
152152
self.console.print(self.config.items(section='profile'))
153153

154-
def _option_points_balance(self):
154+
def option_points_balance(self):
155155
balance = self.api.get_points_balance()
156156
if balance is None:
157157
self.console.print('[bold red]Couldn\'t retrieve the points balance.[/bold red]')
@@ -165,10 +165,10 @@ def _option_points_balance(self):
165165
self.console.print(item['programDisplayInfo']['loyaltyProgramName'] + ': '
166166
'[bold]' + item['programBalance']['balanceDescription'] + '[/bold]')
167167

168-
def _option_clip_coupons(self):
168+
def option_clip_coupons(self):
169169
self.api.clip_coupons()
170170

171-
def _option_purchases_summary(self):
171+
def option_purchases_summary(self):
172172
purchases = self.api.get_purchases_summary()
173173
if purchases is None:
174174
self.console.print('[bold red]Couldn\'t retrieve the purchases.[/bold red]')

0 commit comments

Comments
 (0)