Python Click Part 4 of 4
6:31

Python Click Part 4 of 4

AssemblyAI 09.08.2021 328 просмотров 5 лайков

Machine-readable: Markdown · JSON API · Site index

Поделиться Telegram VK Бот
Транскрипт Скачать .md
Анализ с AI
Описание видео
The fourth and final installation in our guide to Python Click. Python Click is a library for creating command line interfaces. In this last portion, we'll go over how to invoke one command as a subcommand inside of another. For a full text tutorial see https://www.assemblyai.com/blog/the-definitive-guide-to-python-click

Оглавление (4 сегментов)

<Untitled Chapter 1>

all right um now i'm going to go over the last part of the click tutorial where we're going to learn how to use

Invoke Function

the invoke function which lets you run a sub command so what we're going to do is we're going to add an example command and we'll call it get key which we you know we just saw get keys so we're going to get one of these keys basically and it's going to take an argument i'm going to call it uh what should we call this argument uh key name something like that yeah yeah and um we're gonna do click. pass context

Pass Context

so i you gotta mark this with past context so that it knows that the context object can see this and and this knows of the and this can see the context object and it can be called from context um key name yeah um okay so what we're gonna do here is we're just going to get load the dictionary and that's going to be ctx. obj and then we're going to get one of these keys um let's see results oh sorry we're just going to get by key so we're going to do keyname so that's going to be a subject yeah subject is fine and one word is fine um and then we'll just print it out p print dot p print subject um so p print is uh pretty print which just it makes it look a little bit nicer um it formats kind of what json should look like for you uh get key and we'll get um let's get results all right so there you go so this you know p print it prints what it looks like in a nice formatted json file and um yeah so that is what the get key command will do now we're going to do is we're going to get we're going to make something called get

Get Results

results and we're going to do pass context i don't have to do that def get results ctx and that's just going to be so we're going to load it dict equals ctx obj results equals stick dot get uh results and then p print dot p print results and so that should lead us to exactly this thing so instead of get key results we'll just do get results and bam it's the exact same thing so um oh wait no no haha i was gonna do ctx on invoke so we're gonna do ctx. invoke and we're gonna invoke the get key command and we're going to pass it results right yeah i think so um let's do it oh got multiple values for argument key name so actually to get this to work we'll do keyname equals results we have to pass a key named a named parameter in here um yeah i did debug for a second so i went off screen uh let's show you how that works oh yeah i also added this while i was debugging just to show you uh the click. echo and click. style you can do a this is the same thing as click. seco it's just the chain command um so yeah get results will you know get you the get results so python walkthrough walk through get results so it works the same way ctx. invoke right you pass the function you pass the name parameter and that's pretty much most of what you need to know to do um any simple to complex command line interfaces click. group to create you know a group of commands uh pass context in order to pass the context around to your commands arguments in order to have a required mandatory argument options to have a non-mandatory argument um actually i'm going to show you how to pass a yeah uh a flag option here so at click dot option dash f dash flag is flag oops is his flag oh maybe it's is his flag what uh that's weird it should just be is flag and just pass that equal to true and then help equals flag for color of chained echo uh so green flag green if not flag else uh let's come here red so let's pass get keys and let's pass flag there you go so okay this is green but it's supposed to be red okay but yeah just so you know that's how you pass the flag arguments um so options are for optional arguments and um yeah this is pretty much it ctx autofocus for running sub commands and then you just use you know program name and make sure that you call it in main so yeah thanks for watching if you like that like and subscribe and follow for more

Другие видео автора — AssemblyAI

Ctrl+V

Экстракт Знаний в Telegram

Экстракты и дистилляты из лучших YouTube-каналов — сразу после публикации.

Подписаться

Дайджест Экстрактов

Лучшие методички за неделю — каждый понедельник