Skip to content

Commit ca0385a

Browse files
feat: implement [default] section in config
1 parent 405639c commit ca0385a

4 files changed

Lines changed: 84 additions & 10 deletions

File tree

docs/configuration.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Configuration
2+
3+
`gcalcli` supports configuration via a `config.toml` file, typically located at `~/.config/gcalcli/config.toml` (or `$XDG_CONFIG_HOME/gcalcli/config.toml`).
4+
5+
## Structure
6+
7+
The configuration is divided into sections:
8+
9+
### `[auth]`
10+
11+
Settings for authentication (Client ID).
12+
13+
### `[calendars]`
14+
15+
Settings for default and ignored calendars.
16+
17+
### `[output]`
18+
19+
Settings for output formatting (week start).
20+
21+
### `[default]` (New)
22+
23+
Set default values for global command-line flags.
24+
25+
```toml
26+
[default]
27+
interactive = false # Disable interactive prompts (simulates --yes)
28+
color = true # Enable/Disable color
29+
conky = false # Conky compatible output
30+
```
31+
32+
## Example `config.toml`
33+
34+
```toml
35+
[auth]
36+
client-id = "your-client-id.apps.googleusercontent.com"
37+
38+
[calendars]
39+
default-calendars = ["Work", "Personal"]
40+
ignore-calendars = ["Holidays"]
41+
42+
[output]
43+
week-start = "monday"
44+
45+
[default]
46+
interactive = true
47+
color = true
48+
```

gcalcli/argparsers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ def get_argument_parser():
502502
'interactively.',
503503
)
504504
delete.add_argument(
505-
'--iamaexpert', action='store_true', help='Probably not'
505+
'--iamaexpert', action='store_true', help='Legacy alias for --yes'
506506
)
507507

508508
sub.add_parser(

gcalcli/cli.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -193,17 +193,16 @@ def main():
193193
opts_from_config = config.Config()
194194

195195
namespace_from_config = opts_from_config.to_argparse_namespace()
196-
# Pull week_start aside and set it manually after parse_known_args.
197-
# TODO: Figure out why week_start from opts_from_config getting through.
198-
week_start = namespace_from_config.week_start
199-
namespace_from_config.week_start = None
196+
197+
# Apply config values as defaults for the parser.
198+
# This allows config to override program defaults, while still letting
199+
# explicit CLI arguments override config.
200+
parser.set_defaults(**vars(namespace_from_config))
201+
200202
if parsed_args.includeRc:
201203
argv = fromfile_args + argv
202-
(parsed_args, unparsed) = parser.parse_known_args(
203-
argv, namespace=namespace_from_config
204-
)
205-
if parsed_args.week_start is None:
206-
parsed_args.week_start = week_start
204+
(parsed_args, unparsed) = parser.parse_known_args(argv)
205+
207206
if parsed_args.config_folder:
208207
parsed_args.config_folder = parsed_args.config_folder.expanduser()
209208

gcalcli/config.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,30 @@ class OutputSection(BaseModel):
7070
)
7171

7272

73+
class DefaultSection(BaseModel):
74+
model_config = ConfigDict(
75+
title='Default Global Options'
76+
)
77+
78+
interactive: bool = Field(
79+
alias='interactive',
80+
title='Enable interactive mode (default: true)',
81+
default=True
82+
)
83+
84+
color: bool = Field(
85+
alias='color',
86+
title='Enable color output (default: true)',
87+
default=True
88+
)
89+
90+
conky: bool = Field(
91+
alias='conky',
92+
title='Enable conky color codes',
93+
default=False
94+
)
95+
96+
7397
class Config(BaseModel):
7498
"""User configuration for gcalcli command-line tool.
7599
@@ -84,6 +108,7 @@ class Config(BaseModel):
84108
auth: AuthSection = Field(default_factory=AuthSection)
85109
calendars: CalendarsSection = Field(default_factory=CalendarsSection)
86110
output: OutputSection = Field(default_factory=OutputSection)
111+
default: DefaultSection = Field(default_factory=DefaultSection)
87112

88113
@classmethod
89114
def from_toml(cls, config_file):
@@ -98,6 +123,8 @@ def to_argparse_namespace(self) -> argparse.Namespace:
98123
kwargs.update(vars(self.calendars))
99124
if self.output:
100125
kwargs.update(vars(self.output))
126+
if self.default:
127+
kwargs.update(vars(self.default))
101128
return argparse.Namespace(**kwargs)
102129

103130
@classmethod

0 commit comments

Comments
 (0)