You are not logged in.

#1 2024-10-08 08:22:53

fygar256
Member
From: Japan
Registered: 2023-03-03
Posts: 12
Website

simple function to get option for python

---
title: Simple function to get options [python]
tags: Python CLI
author: fygar256
slide: false
---
I made a simple function to get options.

Because the argparse module of the python is complicated.

Name: option
Arguments: (argv, optionname)
Returns: (argv, parameter)

Call it as `(argv, parameter) = option(argv, '-o')`

. argv is sys.argv, and parameter returns the argument immediately after it is specified with '-o'.

A space is required between `-o` and the argument.

When the same option is specified, the first one is valid.

When there are no arguments, it returns a null string to parameter, so if you want to specify multiple identical options, call it repeatedly in a loop.

You can use mine.

Execution example

```
$ python test.py test1 -o test2 test3
(['test.py', 'test1', 'test3'], 'test2')
```

Execution example when different options are specified

```
$ python test.py test1 -a test2 test3
(['test.py', 'test1', '-a', 'test2', 'test3'], '')
```

https://github.com/fygar256/option/

Offline

#2 2024-10-08 22:49:46

fygar256
Member
From: Japan
Registered: 2023-03-03
Posts: 12
Website

Re: simple function to get option for python

Sorry I fixed bug.

Offline

Board footer

Powered by FluxBB